myndcraft Posted September 6, 2008 Share Posted September 6, 2008 I'll preface my first post here with the fact that I am brand spanking new to PHP, I do have familiarity with other programming & scripting languages though so hopefully I will be on the ball here shortly. What I'm trying to do is get a list of all files contained in a folder and it's sub folders. I found this function which works on just the main directory the php file is called from <?php $dirArray = getContents("."); echo count($dirArray); function getContents($curDir){ $openDir = opendir($curDir); while($entryName = readdir($openDir)) { if($entryName != ".DS_Store" && filetype($entryName) != "dir"){ $dirArray[] = $entryName; echo $entryName; } } closedir($openDir); return $dirArray; } ?> So after looking at that and expanding on it a bit I came up with this <?php $dirArray = getContents("."); echo count($dirArray); function getContents($curDir){ $openDir = opendir($curDir); while($entryName = readdir($openDir)) { if(filetype($entryName) == "dir") { $dirArray[] = getContents($entryName); } else { if($entryName != ".DS_Store"){ $dirArray[] = $entryName; echo $entryName; } } } closedir($openDir); return $dirArray; } ?> When I run this though I get a bunch of errors like this Warning: filetype(): Lstat failed for Nike-poster.jpg in /Library/WebServer/Documents/sandbox/test3.php on line 11 Nike-poster.jpg Warning: filetype(): Lstat failed for Nike.mov in /Library/WebServer/Documents/sandbox/test3.php on line 11 Nike.mov The issue here is that the files in question don't reside in the directory tree so somehow we are escaping it. Can anyone provide some insight on this? Thanks! Link to comment https://forums.phpfreaks.com/topic/122955-listing-all-files-in-foldersubfolder-attempting-to-solve-with-recursion/ Share on other sites More sharing options...
corbin Posted September 6, 2008 Share Posted September 6, 2008 Readdir returns . and .. as files, which are the current and level-up folders. That would be why it's going up a folder. Link to comment https://forums.phpfreaks.com/topic/122955-listing-all-files-in-foldersubfolder-attempting-to-solve-with-recursion/#findComment-635027 Share on other sites More sharing options...
myndcraft Posted September 6, 2008 Author Share Posted September 6, 2008 Readdir returns . and .. as files, which are the current and level-up folders. That would be why it's going up a folder. Ugh, thanks corbin ya gotta love those brain dead moments! Link to comment https://forums.phpfreaks.com/topic/122955-listing-all-files-in-foldersubfolder-attempting-to-solve-with-recursion/#findComment-635041 Share on other sites More sharing options...
myndcraft Posted September 6, 2008 Author Share Posted September 6, 2008 Well halfway there... I think. It did work on my test box, but when trying on a live server sandbox I ran into a bunch of errors Warning: filetype(): Lstat failed for (null) (errno=2 - No such file or directory) in /home/httpd/vhosts/domain.com/subdomains/media/httpdocs/test3.php on line 37 Warning: filetype(): open_basedir restriction in effect. File(..) is not within the allowed path(s): (/home/httpd/vhosts/domain.com/subdomains/media/httpdocs:/tmp) in /home/httpd/vhosts/domain.com/subdomains/media/httpdocs/test3.php on line 37 Warning: opendir(Fol name): failed to open dir: Too many open files in /home/httpd/vhosts/domain.com/subdomains/media/httpdocs/test3.php on line 34 Warning: closedir(): supplied argument is not a valid Directory resource in /home/httpd/vhosts/domain.com/subdomains/media/httpdocs/test3.php on line 5 Link to comment https://forums.phpfreaks.com/topic/122955-listing-all-files-in-foldersubfolder-attempting-to-solve-with-recursion/#findComment-635044 Share on other sites More sharing options...
myndcraft Posted September 6, 2008 Author Share Posted September 6, 2008 And because I was running on too little sleep here is the code I was using that worked on the test machine, but produced the errors in the above post. <?php $dirArray = getContents("."); function getContents($curDir){ $openDir = opendir($curDir); while($entryName = readdir($openDir)) { if(filetype($entryName) == "dir" && $entryName != "." && $entryName != "..") { $dirArray[] = getContents($entryName); } else { if($entryName != ".DS_Store"){ $dirArray[] = $entryName; } } } closedir($openDir); return $dirArray; } ?> Link to comment https://forums.phpfreaks.com/topic/122955-listing-all-files-in-foldersubfolder-attempting-to-solve-with-recursion/#findComment-635153 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.