Jump to content

Listing all files in folder/subfolder - attempting to solve with recursion


myndcraft

Recommended Posts

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!

 

 

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

 

 

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;
}
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.