Last updated on January 12th, 2023 at 12:57 pm

PHP has built in functions that can be used for working with files and folders inside a directory.

This script covers these major use cases

  • List all files, No empty / blank area will be displayed on the page during listing.
  • Script has ability to check whether it is a file or directory.
  • Listed files will also have a hyperlink along with their name so that if you have a requirement to pass the filename as query string to another webpage you can easily do that here.
  • If there are no files under a directory then it will display an alert stating no files in the directory.
  • It also count number of files inside a directory.
  • It can also count the number of folders

Let us assume in by document root I have

  • A directory named newdir
  • Inside newdir, we have 4 dummy files with name file1 file2 file3 file4
root@prod-server:/var/www/# ls -rlt
total 8
drwxr-xr-x 3 root root 4096 Jan 12 18:26 newdir
-rwxrwxr-x 1 root root 1301 Jan 12 20:42 list.php
root@prod-server:/var/www/testdir# ls -lrt dir/
total 4
-rw-r--r-- 1 root root    0 Jan 12 18:18 file4
-rw-r--r-- 1 root root    0 Jan 12 18:18 file3
-rw-r--r-- 1 root root    0 Jan 12 18:18 file2
-rw-r--r-- 1 root root    0 Jan 12 18:18 file1
drwxr-xr-x 2 root root 4096 Jan 12 18:26 testdir

As you can see my script named list.php(explained below) is deployed inside /var/www directory

All you have to do is change directory name for $dir_name variable according to your requirement. Make sure you provide name of the directory in this format (/). In my case the the directory I am using is newdir

PHP Code (list.php)

<html>
<body>
	<title>List all files and Directory using PHP</title>
<table id="file_style">
  <tr>
    <th>Folder/File Name</th>
  </tr>
<?php
$dir_name = 'newdir/';
$num_files=0;
$d=0;
?>
<?php  
if ($handle = opendir($dir_name )) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(is_dir($dir_name.$file))
{
$d+=1;
echo "<tr><td><a href='nextpage.php?filename=".$file."'>".$file."(Dir)</a></td></tr>";
//If Directory Do Something Here
}
else
{
$page_name=substr($file, 0, strpos($file, "."));
echo "<tr><td><a href='nextpage.php?filename=".$file."'>".$file."</a></td></tr>";
$num_files +=1;
}
}
}
}
closedir($handle);
echo "<tr><th>Number Of Files $num_files</th></tr>";
echo "<tr><th>Number Of Folders $d</th></tr>";
?>
</table>
<?php if($num_files == 0)
{
echo "No files found in this directory";
}
?>
</body>
</html>

Output of the above script will be

The script works but don’t have any style added. Lets do that by adding some CSS

Add Style

Add the below CSS after the body tag

<style>
#file_style {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#file_style td, #file_style th {
  border: 1px solid #c47979;
  padding: 8px;
}

#file_style tr:nth-child(even){background-color: #f2f2f2;}

#file_style tr:hover {background-color: #ddd;}

#file_style th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4A5B55;
  color: white;
}
</style>

This will make output of the script more beautiful by listing folders and directories with hover effect

Demo

Leave a Reply

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