SPWD

Tag: php file listing script

PHP File Listing Script

by Billy on Oct.28, 2008, under PHP, Web Design

I had a need for a script to read directories for users, listing only files and not sub-directories, so this is the script that ended up being what worked. One of the features was to exclude a few system files on the Linux file system that are present in directories and to list the last modified time so that the users uploading and downloading files could see the different timestamps to differentiate more recent files.

This script handled the uploading of files, after a user was logged into the system. Proper validation and security measures need to be taken before using a script such as this. This may include user logins, ip-restrictions or other measures.

The code explanation follows the code below:

Quick Code


<?php
 
//define the path
//using the "." sets it to the current path
//any relative or absolute path that the web
//server use can read with will work
 
$path = ".";
 
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open files.");
 
echo "<h3>List of items.</h3><p>";
 
//running the while loop
 
echo "<ul>";
while ($file = readdir($dir_handle))
{
//remove . and .. from the list
   if($file!="." && $file!=".." && $file!=".htaccess")
   {  
       $modified = date("F d Y H:i:s", filectime($path."/".$file));
      echo "<li><a href='$path/$file'>$file</a>&nbsp;&nbsp;Last Modified: $modified</li>";
  }
 
}
echo "</ul>";
//closing the directory
closedir($dir_handle);
echo "</p>"
?>
</div>

The script first sets up the directory path that we want to open, in this case I wanted to open the current path, so I used a period to signify the current working directory. Next, we open the directory using the opendir command passing in the $path variable. We have to loop through the entire directory to get all the files. **Note this script does not take in to account sub-directories.**

At this point, the script tests for the directories . and .. and also to not list the .htaccess file if it is present. Within the file check, we get the last modified date of the file, this is done by doing a date format on the filectime function, which accepts the path/filename combination. The script then simply adds each file to an unordered list, and makes each file name (represented by the $file variable) a link so that they can be clicked on to be viewed or downloaded.

The last important line of code closes the directory handle resource by calling the closedir function.

While simple, this script accomplished reading a directory of files and making a linked list of them. As I mentioned I had this was only presented to users who had successfully logged into the system, presenting them with a list of files they could easily download. It was created as a direct replace for an FTP system that was not working well for users - the entire package was a login control, the file listing control (presented here), a way for the user to upload files back and the automatic deletion of files that were older than a set time frame.

I will post about the other parts of the system in the future, and in the end, wrap it up with a complete user managed file transfer system.

Leave a Comment :, , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...