Working with directories
Directories: Open, read, close and view a list of files
An explanation of how to open a directory, read the contents of a directory, and close a directory.
Function opendir (string path)
The opendir() function opens a directory. It takes the path of the directory (can be just the name of the directory) as its parameter, and returns a directory handle that can be used by
the closedir(), readdir(), and rewinddir() functions.
Example:
$dir = opendir("test");
If the opendir() function is successfull you use the readdir() function to read its contents. The readdir() function takes the directory handle and returns any files and sub-directories individually.
Function readdir (resource dir_handle)
The readdir() function accepts the directory handle and as said above, returns the name of the NEXT file/sub-directory in the directory in no particular order.
Example:
$file = readdir($dir);
Note: readdir() returns only one file at a time, a while-loop is used to get all the files.
Readdir() returns the current directory (.) and the parent directory (..). If you do not want to list these directories you can strip them out:
Function closedir (resource dir_handle)
The closedir() function uses the directory handle to close the directory stream opened by the opendir() function.
Example:
closedir($dir);
We can use these functions together to get a list of files in a certain directory.
Note: any sub-directories in the directory that you want to get a list of should be world readable (CHMOD 777).
PHP Code:
<?php // List files in the current directory and their size $Open = opendir("directory"); //replace 'directory' with the name of a directory while($Files = readdir($Open)){ $Filename = "directory/" . $Files; if(is_file($Filename)) { $Size = filesize("directory/$Files"); print("$Filename - $Size bytes \n"); } } closedir($Open); ?>
PHP Code:
<?php // List all files in the current directory and strip out . and .. $dir = opendir('.'); while (($file = readdir($dir)) !== false) { if ($file != "." && $file != "..") { echo "$file\n"; } } closedir($dir); ?>
Note: !== did not exist until 4.0.0-RC2 so if using an version of PHP do "while ($file = readdir($dir))"
The files found by the readdir() function are not returned in order. If you put the files returned by the function into an array, you can use the sort() function to return them in order. Keep in mind that A
through Z takes precedence over a through z! Meaning Zebra.gif will appear before aardvard.gif. Use natcasesort($array)natcasesort -- Sort an array using a case insensitive "natural order" algorithm
PHP Code:
<?php //Load directory into array $dir=opendir('.'); while (($file = readdir($dir)) !== false) { $dirlist[] = $file; } //Close directory stream and sort closedir($dir); sort($dirlist); //foreach ($files as $file){ echo $file; } while (list($key, $val) = each($dirlist)){ if ($val != "." && $val != "..") { echo '<a href="'.$val.'">'.$val.'</a><br>'; } } ?>
Seeing a list of all the files in a directory is good, but sometimes there are subdirectories that could also have files or more directories. You can write a recursive function that would give you a list of all the files in a directory and also in any directories in that directory.
PHP Code:
<?php //Recursively print a list of files in a directory function getDirList ($dirName) { $d = dir($dirName); echo 'Directory handle: '.$d->handle." "; //print the directory handle echo 'Directory path: '.$d->path." "; // print the directory path echo "Files:<br>\n <ul>"; while($entry = $d->read()) { if ($entry != "." && $entry != "..") { if (is_dir($dirName."/".$entry)) { echo 'Directory <a href="'.$dirName.'/'.$entry.'/">'.$entry."/</a><br>"; getDirList($dirName."/".$entry); } else { echo '<li><a href="'.$entry.'">'.$entry."</a><br>"; } } } echo "</ul>"; $d->close(); } if ($dirtopen) { if($direct !=""){ getDirList($direct); }else{ getDirList("."); //if directory is not entered, print list of the current directory } } // enter name of directory in form, should be the name of the present directory, // or a subdirectory in the present directory PRINT "<form action=\"dirlist.php\">"; PRINT "<INPUT TYPE=\"text\" NAME=\"direct\" SIZE=\"15\">"; PRINT "<br>"; PRINT "<INPUT TYPE=\"submit\" NAME=\"dirtopen\" VALUE=\"OpenDir\">"; PRINT "</FORM>"; ?>