Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Viewing All Files in a Directory

Viewing All Files in a Directory

Learn how to create a simple script to view the contents of a directory.


PHP can do many, many things, of course everyone knows of MySQL interaction, but on its own PHP can do almost anything, with very little assistance from other programs or database engines. Now the reasons you may need to view all the files can vary. You may need it to view all the images in a directory in an organized manner(instead of just the default "no index page found" page), or you may need to include all the files in a directory; Whatever your reason, this is viewing all the files in a directory.

The first thing that i must say is that there are 2 ways to do this, the way i call the "long way" and the way i prefer. I'll show you both, just to give you an idea on the subject.

So first what we need to do is choose the directory, so for me i will choose ./ , which if you don't know "./" is computer for "the current directory we are working in." Next, we somehow or another open a directory, which is made easy by the opendir() function. Afterwards we loop through the directory until all the files(and subfolders) are done. When you put this into php code, you get something along the lines of this :

 $directory = "./";
 //Directory is set;
 $dir = opendir($directory);
 //Opened the directory;

 while($file = readdir($dir)){
  //Loops through all the files/directories in our directory;
    if($file!="." && $file != "..")){
      echo $file."<br/>\n";

How does this work?

One thing you may be asking yourself is "why the IF inside the loop?" Good question, and the answer is that i see one downfall with the readdir() method; this is the fact that not only does it read out all the subdirectories and files, it also outputs the "./" directory, and the "../" directory, which is computer for "the folder that this folder is in," and if you are running code where you don't want the top directory accessible, this could present problems.

Next is the method i prefer; it uses a function called glob(); so in english what we have to do for this method is declare a directory we want read, check if the wild-cards operator is present, loop through the directory, and finally, print out the file name. Once we put that in code, we get this:


$directory = "./";
//Directory is set;

$directory = (!strstr($directory,"*") || $directory =="./" ) ? $directory."*" : $directory;
//Checks if the wildcard operator is present, and if not it adds it by default at the end;

$files = glob($directory);
//Yes, it was that easy to get all the files;

for($i=0;$i<sizeof($files) ; $i++){
  //Loop through the files;

 echo basename($files[$i])."<br/>\n";


You must run things through the basename() function, otherwise you will get things like /var/www/root/~user/file.php, when you just want file.php to be displayed;

Understanding the glob() function

Each way works, its just that i use the glob() function because it requires less code to do a partial check. What i mean is that, lets say i have a directory filled with 400 .html files, and 10 .php files, i just want to display the .php files. With the readdir method it takes a lot of if statements and double checking, but with the glob method its just a matter of setting $directory = "*.php"; and letting the code run; so i leave it to you to decide which one you want to use.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Viewing All Files in a Directory

 Kevin  Sat Nov 22, 2008 6:35 pm  
I have gotten this script to work for files in the same folder, but not others.... How can I apply this to files in a different folder?
 coder  Tue Dec 9, 2008 10:58 am  
Maybe with this line

$directory = "./";
 Brandon  Fri Mar 13, 2009 10:36 am  
Thank you!!!!!

This code is what I have been looking for, for a long time.

Thank you so much for this post!
 anny  Wed Sep 29, 2010 2:02 am  
anyone tell me how to read an excel file in php
 Tony  Sun Sep 4, 2011 2:15 pm  
Hi there, thank you very much for this script! Can i ask you if it is possible somehow to upgrade this script to show only specific number of lately added files? for example five latest?

And second thing, it is somehow possible to print the files as a links to themselves?

It is just a question, if it is completely different stuff, let it be. But itīs much easier to ask somebody who understand the whole thing.

Thanks very much.

Add a comment
The ability to add new comments has been disabled in this archived version.

Main | Tutorials | Articles