mamuscia
asked on
Using PHP5 DirectoryIterator to return all files and folders in a directory
I've looked for answers, but none of those I found had the "simple" answer I'm looking for.
I have a directory on my web server that contains both files and sub-directories with files in them as well.
I would like to build a tree-like structure from the top directory on down and I understand that the DirectoryIterator class will do this, but I just don't understand enough PHP to pull a function together to do this.
I would like to get all files and sub-folders and their contents back in an array if possible so that I can create my own HTML output in a tree-like manner. I would like to get information for the files that are returned, such as type, size, filename as well. Can someone share the code for this using the DirectoryIterator class?
thanks
I have a directory on my web server that contains both files and sub-directories with files in them as well.
I would like to build a tree-like structure from the top directory on down and I understand that the DirectoryIterator class will do this, but I just don't understand enough PHP to pull a function together to do this.
I would like to get all files and sub-folders and their contents back in an array if possible so that I can create my own HTML output in a tree-like manner. I would like to get information for the files that are returned, such as type, size, filename as well. Can someone share the code for this using the DirectoryIterator class?
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bah! the code snippit formatting looks terrible lol
ASKER
I tried the code there and it does not produce any results. I tried changing the directory a few times to different values and still nothing. If I can get this to work, it should provide what I'm looking for.
<?php
error_reporting(E_All);
$dir = new DirectoryIterator( './zones' );
foreach($dir as $file )
{
if(!$file->isDot() && !$file->isDir()) {
if ($file->isDir()) {
echo "Directory: ".$file->getFilename();
}
else {
//if ($file->isFile()) {
echo "FileName: ".$file->getFilename();
echo "Size: ".number_format(($file->getSize()/1024),2)." Kb";
echo "Date Created: ".date("D d M Y H:i:sa",$file->getCTime());
echo "Type: ".$file->getType();
}
}
}
?>
hmm the code I wrote based off that site worked fine
using PHP 5.2.5
$dir = new DirectoryIterator( './zones' );
perhaps set that to the full path ie
'/right/from/root/of/the/s erver/'
using PHP 5.2.5
$dir = new DirectoryIterator( './zones' );
perhaps set that to the full path ie
'/right/from/root/of/the/s
ASKER
OK, OK....I set the directory wrong. It's now working. I also changed the IF statement to remove the check for isDir so it only checks on the first IF for !isDot(). That way, the IF's below check if it's a file or directory.
It's working great now.
By the way WILDZERO I tried your code too and it works. I am going to split the points with each of you if you don't mind. Thanks for your quick replies.
It's working great now.
By the way WILDZERO I tried your code too and it works. I am going to split the points with each of you if you don't mind. Thanks for your quick replies.
ASKER
Oops.....I meant you'll get all the points.
>> I am going to split the points with each of you if you don't mind.
lol, it was just me that replied :P
lol, it was just me that replied :P
ASKER
Nice!
Thanks :-)
ASKER
Yeh, I saw MASTER and mis-interpretted it for another ID....:-)
ASKER
Hey Wildzero if you or anyone else is still listening. The function I use stops at the first level directory and does not retrieve files and folders below where I point it. Do you know of anything that would limit the recursion to just one level? For example, if I point it at "./", it retrieves just the files and directory names found in that directory, but does not open all directories to look inside of them. Do you know what I have to do to get all files and folders from a given directory - AND all of their children file and folders, etc? Thanks.
yes, see my code - it did recurrision
ASKER
I see that, thanks again.
Enjoy
Open in new window