Archive for the ‘Beginner’ Category
Loop through a directory displaying the files.
I’ve needed this a a few time, whether it was just to list a directory while limiting the output or puting files in a directory into an XML format. This short script can be built on to suit your needs. This particular script will do the following:
- Open the directory
- Loop through each file
- Use an echo in the loop displaying the directory and file names
- Close the directory when finished
The script:
$dir = opendir('.');
while (false !== ($file = readdir($dir))) {
if ( ($file != basename($_SERVER['PHP_SELF'])) && ($file != '.') && ($file != '..')) {
if(is_dir($dir->path.$file)) {
echo “Directory: $file<BR>”;
} else {
echo “File: <a href=\”$file\”>$file</a><BR>”;
}
}
}
closedir($dir);
?>
Lets break it down..
This line opens the directory specified an pulls it into a variable. using “opendr(’.')” lets us open the directory that the script is being run from. You can use the absolute path to a directory if you wanted to run the script from a different location.
$dir = opendir(’.');
This is our loop which will go through each file and directory and we have several conditions inside this loop to format the output.
while (false !== ($file = readdir($dir))) {
This line will stop the loop from displaying specified directories and files. basename($_SERVER[’PHP_SELF’]) is a PHP function that will return only the filename. You could have specified the filename here, but this makes this small script a little more portable. If there are other files or directories that you don’t want to include, you can add them here and they won’t appear in your list.
if ( ($file != basename($_SERVER[’PHP_SELF’])) && ($file != ‘.’) && ($file != ‘..’)) {
This next line almost explains itself using the is_dir() function. It checks to see if the current file/folder is actually a directory, we have this inside our condition so that we can output the file name in the following line. You could make this output into a clickable link by adding a href tag. Of course if you don’t want to show directories you can exclude this portion of the statement.
if(is_dir($dir->path.$file)) {
echo “Directory: $file<BR>”;
If it isn’t a directory, lets do something else!
} else {
Fore files I have the script output a clickable link, close the else, if, and while brackets.
echo “File: <a href=\”$file\”>$file</a><BR>”;
}
}
}
And now close the directory when we are finished.
closedir($dir);
The script has alot of possibilities, you could add some SQL and have it catalog a directory allowing you to compare changes and whatnot. There are other file functions you can add to display file size and other information. I had a simular script back in 2002 but have since lost that one. This was a pretty quick put together, but I may ressurect the other one due to usefulness. You could also add images to make it a better looking directory listing with different images per filetype. Motivation for this little tutorial was thanks to Nick.