HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [234]
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:\xampp\htdocs\XFD\xfd5.6 directory:
chdir(“C:\xampp\htdocs\XFD\xfd5.6”);
When you change to a directory; you’re then free to write to it with the fwrite() function. See the “fwrite( )” section, earlier in this chapter.
Generating 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; see Figure 6-7 for the HTML generated by this example:
PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
- $aFile \n”;
$dp = opendir(”.”);
$currentFile = ””;
while($currentFile !== false){
$currentFile = readDir($dp);
$filesArray[] = $currentFile;
} // end while
//sort the array in alpha order
sort($filesArray);
$output = ””;
foreach($filesArray as $aFile){
$output .= ”
} // end foreach
print ”$output”;
?>
Figure 6-7:
A list of links to all files in the directory specified by the opendir() function.
Here’s how the fileList.php program performs its magic:
1. Open a directory pointer to the current directory.
In all major operating systems, the period (.) character indicates the current directory.
$dp = opendir(“.”);
2. Build a loop that repeats until there are no files left.
The special !== comparison is used to prevent rare problems, such as files named false. (Yes, believe it or not, that sometimes happens.)
while($currentFile !== false){
3. Read the next file from the directory pointer.
The readDir() function reads the next file in and stores it to a variable ($currentFile).
$currentFile = readDir($dp);
4. Append the current file to an array.
If you simply assign a file to an array without an index, PHP places the element in the next available space.
$filesArray[] = $currentFile;
5. Sort the array.
The files won’t be in any particular order in the array, so use the sort() function.
sort($filesArray);
6. Print each element of the array.
I use an unordered list of links to display each file. Make it a link so that the user can click the file to view it directly.
foreach($filesArray as $aFile){
$output .= “
} // end foreach
On a Windows server, you have to escape the backslashes in the file path. You do this by adding a backslash before the backslashes in the file path. (For example, you would write C:\\xampp\\htdocs\\XFD\\xfd5.7\\ instead of C:\xampp\htdocs\XFD\xfd5.7\.) On a Unix server, you don’t have to do this because file paths use slashes (/) instead of backslashes (\).
If you want just one particular file type, you can use regular expressions to filter the files. If I had wanted only the .txt and .dat files from the directory, I could have run the files array through this