macattack Posted May 23, 2008 Share Posted May 23, 2008 I have an issue with a scandir() function that I'm trying to implement on a blog. I want the function to look for the latest posts, and then display links to them. The code won't look in all subfolders, however. The directory is set up this way: "../blogs/YYYY/MM/DD/" the function is currently returning a hidden file, and the two months where I've put in trial posts. I want it to ignore hidden files (the file in question is .DS_Store) and get past all the subfolders to access the posts. Here is the code: function listBlogs($dir=''){ $files = scandir(DIRECTORY_LISTING."/$dir", 1); foreach($files as $file){ if($file == "." || $file == ".." || $file == ".svn") continue; if(is_dir(DIRECTORY_LISTING."/$file")){ if($dir != '') $recurse = "$dir/$file"; else $recurse = $file; listBlogs("$recurse"); } else{ list($month, $day, $hour, $minute) = sscanf($file, "%s %d %d %d %d"); echo "<a href='index.php?blog=$dir/$file&title=$file'>$file</a><br>"; } } } Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/ Share on other sites More sharing options...
phpzone Posted May 23, 2008 Share Posted May 23, 2008 check the php manual for recursive directory scanning, there are comments on the page by users. http://uk3.php.net/scandir You can check whether the files begins with '.' by doing $ishidden = substr( $file, 0, 1 ) == '.' ? true : false; Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/#findComment-548166 Share on other sites More sharing options...
947740 Posted May 23, 2008 Share Posted May 23, 2008 If you want to check out a good (I think ) scandir function, go here: http://www.phpfreaks.com/forums/index.php/topic,197539.msg891381.html#msg891381 Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/#findComment-548179 Share on other sites More sharing options...
macattack Posted May 23, 2008 Author Share Posted May 23, 2008 I've modified my code considerably after reading the comments on the PHP manual. As much as I appreciate the offer of code from 947740, it would defeat my purpose of understanding what to do. I now get the dialog "Catchable fatal error: Object of class Directory could not be converted to string in /Users/~username/Sites/PHP Blog/includes/functions.php on line 13". Any further help would be appreciated. Please keep in mind, I'm trying to search for all posts under "blogs", (../blogs/YYYY/MM/DD/) <?php define('BLOG_NAME',"Test Blog",'TRUE'); function listBlogs($dir=''){ $files = scandir("../$dir", 1); $dir = dir("../$file"); while(($file = $dir->read()) !== false){ if(is_dir($dir->path.$file) == true && $file != '.' && $file != '..' && $file != '.svc'){ list($date) = sscanf($file, "%s %d"); echo "<a href='index.php?blog=$dir/$file&title=$file'>$file</a><br>"; } } $dir->close(); Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/#findComment-548370 Share on other sites More sharing options...
rhodesa Posted May 23, 2008 Share Posted May 23, 2008 echo "<a href='index.php?blog={$dir->path}/$file&title=$file'>$file</a><br>"; Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/#findComment-548380 Share on other sites More sharing options...
macattack Posted May 23, 2008 Author Share Posted May 23, 2008 Thanks for the help, that removed the error warning. However, it is currently searching at the level "YYYY", and then listing that as the file to open. I want it to look for each value for "YYYY", then each subsequent value at each subsequent level. It's at the DD/ level where the posts are located. Can anyone suggest how to automatically search to that level? Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/#findComment-548390 Share on other sites More sharing options...
macattack Posted May 24, 2008 Author Share Posted May 24, 2008 Perhaps the second portion of the question wasn't stated clearly. My posts are in the folder "blogs/YYYY/MM/DD/" with YYYY being the 4 digit year, MM the two digit month and DD being the two digit day. For example, an entry for today would appear as "blogs/2008/05/24/blogname.txt". I want the scandir() function to look past the level that the year is on, and search all the way down to the entries. Right now, the function returns each year that a post has been made in. Here is the current function: function listBlogs($dir=''){ $files = scandir("blogs/$dir", 1); $dir = dir("blogs/$file"); while(($file = $dir->read()) !== false){ if(is_dir($dir->path.$file) == true && $file != '.' && $file != '..' && $file != '.svc'){ list($date) = sscanf($file, "%s %d"); echo "<a href='index.php?blog={$dir->path}/$file&title=$file'>$file</a><br>"; } } $dir->close(); Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/#findComment-549120 Share on other sites More sharing options...
macattack Posted May 24, 2008 Author Share Posted May 24, 2008 Alright, after some searching and code altering, I've found a solution that gets me almost where I need to be. This part of the question has been answered. Here's the offending code, for future reference: function listBlogs($dir='blogs'){ foreach(scandir($dir) as $entry) if($entry != '.' && $entry != '..' && $entry != '.svc') { $entry = $dir.'/'.$entry; if(is_dir($entry)) { $path = pathinfo($entry); $listarray[$path['blogs']] = listblogs($entry); } else { $path = pathinfo($entry); $listarray[] = $path['blogs']; } } } Link to comment https://forums.phpfreaks.com/topic/106948-solved-scandir-issue-not-looking-in-all-subfolders/#findComment-549187 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.