HTML5 and CSS3 All-in-One For Dummies
Book image
Explore Book Buy On Amazon

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:



 
 fileList.php


 

About This Article

This article is from the book:

About the book author:

Andy Harris taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: