10 tips for PHP scripts: Maintain your file system with PHP

Learn how to create a basic directory listing and copy a file with PHP.

By Julie Meloni

PHP has numerous functions relating to your file system, allowing you not only to open files, but also display the contents of directories, move files around, and much more. Many people even write Web-based file explorers in PHP.

First, a note regarding file paths: On Windows, you can use both the forward slash (/) and the backslash (\) in file paths, whereas other operating systems use only the forward slash. For consistency's sake, these examples will use the forward-slash method.

This sample script shows you a basic directory listing. Comments are inside the code, explaining each step:

<? /* Put the full path to the directory you want to read, in a variable called $dir_name */

$dir_name = "/home/me/";

/* Create a handle, the result of opening the given directory */

$dir = opendir($dir_name);

/* Start a text block, into which you'll place the list elements (your filenames) */

$file_list = "<ul>";

/* Using a while statement, read all of the elements in the open directory. If the name of the file is not "." or "..", print the name in your list */

while ($file_name = readdir($dir)) {
if (($file_name != ".") && ($file_name != "..")) {
$file_list .= "<li>$file_name";

/* Finish up your bullet list */

$file_list .= "</ul>";

/* Close the open directory handle and end your PHP block*/



<!— Start your HTML —>

<TITLE>Directory Listing</TITLE>

<!— Use PHP to print the name of the directory you read —>

<P>Files in: <? echo "$dir_name"; ?></p>

<!— Use PHP to print the directory listing —>
<? echo "$file_list"; ?>


Voilà, you have a directory listing. Remember, to read the contents of a directory or a file (which you'll see in a moment), the user under which PHP runs must have at least read permissions for that directory or file.

This next example shows how to copy a file:

<? /* Put the full path to the file you want to copy in a variable called $original, and the full path to the copied file in a variable called $copied */

$original = "/home/me/mydatabasedump";
$copied = "/archive/mydatabasedumo_1010";

/* use the copy() function to copy the original to the new, or die and print an error */
@copy($original, $copied) or die("Couldn't copy file.");


This example script can be the first step in a rudimentary backup system; when the script is run, it copies a database dump to a different location for safekeeping. With a modification to your crontab, you can execute this file at a time of day that you choose; this requires no user intervention.

Assuming you have Lynx on your system, you can create a crontab entry to fire off Lynx and access this file. Accessing the file will run the script and create the copied file. The example below will run the script at 5 a.m., and then shut Lynx back down:

0 5 * * * [username] lynx -dump http://localhost/copyfile.php 1>/dev/null 2>&1

If you're running the CGI version of PHP, you can skip the Lynx part altogether, and reference the binary file:

0 5 * * * [username] php /path/to/copyfile.php 1>/dev/null 2>&1

Julie Meloni is the technical director at i2i Interactive and is an avowed proponent of Linux and the open source community. A regular contribtor to CNET, she has written a few books on PHP and other technologies.

Editor's Picks

Free Newsletters, In your Inbox