How to Use PHP File and Directory Functions for HTML5 and CSS3 Programming - dummies

How to Use PHP File and Directory Functions for HTML5 and CSS3 Programming

By Andy Harris

Sometimes, you may need PHP to work with files in a directory for HTML5 and CSS3 programming. Say you have a reporting tool for a client. Each week, you generate a new report for the client and place it in a directory.

You don’t want to have to alter the page each time you do this, so instead, make a page that automatically generates a list of all the report files for the client to select from. This is the kind of thing you can do with functions like opendir() and readdir().

opendir( )

Using the opendir() function, you can create a variable (technically speaking, this type of variable is called a handle) that allows you to work with a particular directory.

The opendir() function takes one parameter: the path to the directory you want to work with. The opendir() function returns a directory handle (kind of like a connection to the directory) on success and False on failure.

Here is an example of the opendir() function. This function stores a directory handle to the C:xampphtdocsXFDxfd5.7 directory in the $directoryHandle variable:

$directoryHandle = opendir("C:xampphtdocsXFDxfd5.7");

readdir( )

After you open the directory with the opendir() function, you have a cursor pointed at the first file. At this point, you can read the filenames one by one with a while loop. To do this, use the readdir() function.

The readdir() function takes one parameter: the variable containing the directory handle created with the opendir() function. The readdir() function returns the name of a file currently being focused on by the cursor on success and on failure.

Here is an example of the readdir() function. This function iterates through each file in the directory specified by $dp and assigns the filename of the current file to a new index in $fileArray array:

while($currentFile !== false){
 $currentFile = readDir($dp);
 $filesArray[] = $currentFile;
}

The actual readdir() function itself is readdir($dp). For more on the function, see the official PHP online documentation at http://us.php.net/function.readdir.

In some circumstances, the readdir() function might return non-Boolean values which evaluate to False, such as 0 or “”. When testing the return value of the readdir() function, use === or !==, instead of ==or !==, to accommodate these special cases.

chdir( )

If you want to create a file in a directory other than the directory that the PHP page creating the file is in, you need to change directories. You change directories in PHP with the chdir() function.

If you want to be absolutely sure that you’re in the right directory before writing the file, you can use an if statement with the getcwd() function. This is usually a bit of overkill, but it can be helpful.

The chdir() function takes one parameter: the path to the directory you wish to work with. The chdir() function returns True on success and False on failure.

Here is an example of the chdir(). This function changes to the C:xampphtdocsXFDxfd5.6 directory:

chdir("C:xampphtdocsXFDxfd5.6");

When you change to a directory, you’re then free to write to it with the function.

How to generate the list of file links

Using the opendir() and readdir() functions, you can generate a list of links to the files in a directory.

Take a look at the PHP code for the file links list example:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>fileList.php</title>
</head>
<body>
<ul>
<?php
$dp = opendir(".");
$currentFile = ";
while($currentFile !== false){
$currentFile = readDir($dp);
$filesArray[] = $currentFile;
} // end while
//sort the array in alpha order
sort($filesArray);
//write the output
$output = ";
foreach($filesArray as $aFile){
$output .= " <li><a href="$aFile">$aFile</a></li> n";