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. ?>
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

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

Back To Top