List the content of a directory/folder using PHP

List the content of a directoryfolder using PHP

With a few lines of code and a while loop you can list the content of a directory on both Unix and Windows machines.

1. <?php
2. // The path that we're interested in
3. $Folder = "C:\\";
4. // Open the folder
5. $DirHandle = @opendir($Folder) or die($Folder." could not be opened.");
6. // Loop through  all the files in the directory
7. // Unix systems will also have . and .. as the path to the parent and root directory
8. while ($Filename = readdir($DirHandle))
9. {
10.        echo $Filename."<br />";
11. }
12. // Close the handle to the directory
13. closedir($DirHandle);
14. ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top