How to List All Files of a Directory Using PHP

PHP




Enter a Directory: Enter Toys or Vacations directory








In this article, we show how to show all files in a directory of a website using PHP.

The above HTML form allows you to see the Toys directory or the Vacations directory of this website.

If you enter in "Toys" or "Vacations" and press the 'Submit' button, you will see all files for the associated directories.

And the form above is made so that links are created for all the files in the directories. So by clicking the link, you can see the contents inside all the files.

This is actually really much like an file transfer protocol (FTP), which allows you see all files in all directories of your website.

So this form above can kind of be seen as an FTP on the cloud that you can access from your own website.

You have access to all the files and see if you right click on the link, you can save it to the computer.

So PHP is pretty efficient at looking and obtaining files you may need for any directory on your website.


PHP Code

So now we will go into the code required to display all files in a directory.

The PHP code needed to show all files in a directory is shown below.



The PHP code above displays all the files in the Toys directory. This is shown below.

filename:Helicopter.jpg
filename:.
filename:Popular-toys.txt
filename:Truck.jpg
filename:Coloring-book.jpg
filename:index.php
filename:default2.css
filename:Drone.jpg
filename:Nerf-gun.jpg
filename:Teddy-bear.jpg
filename:Remote-controlled-plane.jpg
filename:..


You can see how every file in the Toys directory is listed below.

We'll now explain the code of how this is done.

So the first line specifies the path to the directory, whose file contents we want to find.

Since we want the files listed in the Toys directory, we set the directory equal to "Toys/"

Depending on your website set up, based on the current directory you are in, you may have to set the directory equal to "/Toys". Most of the time, however, "Toys/" would be the correct setup.

This, again, is if you want to list all the files in the Toys directory.

If you want to list all the files in the images directory, you would set directory equal to "Images/"

If you want to list all the files in the Files directory, you would set directory equal to "Files/"

The next line is an if statement that makes sure that the pathway you specified is a directory. The is_dir() function checks to make sure that this pathway is a directory. If it is, true is returned. If it isn't, false is returned and the code does not advance.

The next if statement opens the directory if it is in fact a directory. If it is and it opens, the $opendirectory variable returns true. If it isn't, the $opendirectory variable returns false and the code does not advance.

The next statement, which is a while statement, reads the directory. The readdir() function reads the files from the directory. So as long as there are files, the while statement is true and keeps returning files until all files are returned. During each file, we echo out the filename. When the while statement finishes executing, the while loop is exited and the directory is closed.

Notice that all the file names in the Toys directory are listed, but they aren't hyperlinks. They're simply static text. Below we'll show how to make the file names hyperlinks so that we can we can link and see the contents of them.


How to List All Files the Home Directory of Your Website

To list all sites in your home (or root) directory is different than all other directories. This is because the home directory has no name, like all other directories. Therefore, specifying its path in the code is different. To specify the home directory, we have to set the $directory variable equal to ".". This makes the PHP code search the home directory.

The PHP code to list all files in the home directory is shown below.



So you can see that all the code is the same as before, except the $directory variable is set to "."

This makes the PHP code output everything in the home directory.


List All Files in a Directory With Hyperlinks

The PHP code below now shows how to show all the files in a directory with hyperlinks, so that you can actually click the files and see the contents of them.



The code above produces the following files shown with hyperlinks below.

Helicopter.jpg
.
Popular-toys.txt
Truck.jpg
Coloring-book.jpg
index.php
default2.css
Drone.jpg
Nerf-gun.jpg
Teddy-bear.jpg
Remote-controlled-plane.jpg
..


You can now see all the files in the Toys directory shown with hyperlinks. And you can now click them and see the contents of them.

If you want to show hyperlinks of the home directory, you woudl have to change the line, $directory= ".";, and you would have to change the line, echo "<a href='http://www.learningaboutelectronics.com/" . "$file'>$file</a>"."<br>";


If you want the files to appear in its own separate window, you can modify the above code so that it will now be like as shown below.



So now the files, once clicked, will appear on a new separate window.


If you want to know how to build a system such as shown above where you can type in which directory you would like to see the contents of, see the following link: Search All Files in a Directory.This link contains the full HTML and PHP code that you need to show the files of any directory. Copy the contents of this file and save it in a PHP file (.php).

So using this simple PHP code, we can display all the files of a directory using PHP.


Related Resources

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Upload Images to a Website Using PHP

How to Redirect to Another URL with PHP

How to Create a Search Engine Using PHP

How to Insert Images into a MySQL Database Using PHP

How to Create a Searchable Database Using MySQL and PHP

How to Search a MySQL Table For Any Word or Phrase Using PHP

How to Create a Send Email Form Using PHP and MySQL

How to Create a Newsletter from Scratch using PHP and MySQL

How to Create a Register and Login Page Using PHP