PHP for the World Wide Web (Visual QuickStart Guide)

I l @ ve RuBoard

There are a few useful file and directory functions built-in to PHP which I'll cover in brief here. They include renaming and deleting files as well as listing files located in a directory. I'll review the syntax for some of them first and then demonstrate how they function within the context of a very useful PHP script.

First, there is the rename() function, which works as you would expect it to:

rename ("old name", "new name");

It can be used on either files or directories.

Another function which will be used in this section is filesize(), which determines how large a file is in bytes. This value can be assigned to a variable or printed.

$Number = filesize ("filename");

One important concept that I'll demonstrate in the following script involves listing all of the files found within a directory.Such a script allows you to view directories without FTP access and can be used for creating an online document repository. Reading through a directory is quite similar to reading from a file. First you open the directory, then you look at the files one at a time, and lastly you close the directory. Instead of using a file pointer, as you did when reading files, you use a directory handle, but the concept is the same.

$Handle = opendir ("path"); readdir ($Handle); closedir ($Handle);

Since the readdir() function identifies one file at a time, you'll need to place it in a loop, like this:

while (readdir ($Handle)) { statements; }

You'll put all of these capabilities together into one page which will constitute a Web-based control panel for viewing and managing directories.

To create the directory control panel:

  1. Open your text editor and begin a new PHP document.

  2. Create the standard HTML header (Script 10.8).

    Script 10.8. Instead of using functions, this page uses conditionals, but the results are comparable. To test your knowledge, you might want to rewrite this using functions, especially the section that displays the files within a directory.

    <HTML><HEAD><TITLE>Viewing Files in a Directory</TITLE></HEAD><BODY>

  3. Begin a table and then start the PHP section.

    <TABLE BORDER=0 WIDTH="60%" CELLSPACING=2 CELLPADDING=2 ALIGN=CENTER> <?php

    In the interest of making the page more attractive and orderly, the data will be formatted within a table.

  4. Write a series of conditionals testing whether or not to perform certain acts based upon the existence of certain variables : $Upload (uploading files), $Rename (renaming files), and $Delete (deleting files).

    if ($Upload) { print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Uploaded file name: $File_name</TD></TR>\n"); print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Uploaded file size: $File_size</TD></TR>\n"); if (copy ($File, "users/$File_name")) { print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Your file, $File_name, was successfully uploaded! </TD></TR>\n"); } else { print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Your file, $File_name, could not be copied. </TD></TR>\n"); } unlink ($File); print ("<TR><TD COLSPAN=4 ALIGN=CENTER>&nbsp;</TD></TR>\n"); }

    The $Upload variable will be set if the user uploaded a file. Hence, if $Upload exists, the uploaded file will be handled as you've seen previously.

    The last print statement, which will create a blank table row, is there just for aesthetic purposes only, a theme which is continued through the next two conditionals as well. The extra space that line of code creates will make a more visually appealing Web page.

  5. if ($Delete) { for ($i = 0; $i < count ($Delete); $i++) { if ( unlink ("users/$Delete[$i]") ) { print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Your file, $Delete[$i], was successfully deleted!</TD> </TR>\n"); } else { print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Your file, $Delete[$i], could not be deleted.</TD> </TR>\n"); } } print ("<TR><TD COLSPAN=4 ALIGN=CENTER>&nbsp;</TD></TR>\n"); }

    You'll use a variable to identify whether or not any files need to be deleted, as you did with uploaded files. Since multiple files could be deleted, an array has been created and the loop will handle each array element by unlinking them in turn .

    Figure .

  6. if ($Rename) { for ($n = 0; $n < count ($Rename); $n++) { $OldFilename = $Rename[$n]; $Old = "users/$OldFilename"; $New = "users/$NewName [$OldFilename]"; if ( rename ($Old, $New) ) { print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Your file, $Rename[$n], was successfully renamed!</TD> </TR>\n"); } else { print ("<TR><TD COLSPAN=4 ALIGN=CENTER>Your file, $Rename[$n], could not be renamed.</TD> </TR>\n"); } } print ("<TR><TD COLSPAN=4 ALIGN=CENTER>&nbsp;</TD></TR>\n"); }

    The same is true for renaming files as it is for deleting with respect to walking through the array elements. Once you've assigned the file's new and old names , a call to rename() will handle the alterations.

  7. Create the HTML form, being sure to include the ENCTYPE code to allow for file uploads.

    print ("<FORM ACTION=\"files.php\" METHOD=POST ENCTYPE=\"multipart/ form-data\">\n");

  8. Print out the table headers.

    print ("<TR><TD><B>File Name</B> </TD><TD><B>File Size</B> </TD><TD><B>Delete</B></TD><TD> <B>Rename</B> (Enter the New Name in the Box)</TD></TR>\n");

    Figure .

  9. Write the code to read from the directory.

    $Open = opendir ("users"); while ($Files = readdir ($Open)) { $Filename = "users/" . $Files; if (is_file ($Filename)) { $Size = filesize ("users/$Files"); print ("<TR><TD>$Files</TD> <TD>$Size</TD><TD><INPUT TYPE=CHECKBOX NAME=\"Delete[]\" VALUE=\"$Files\"></TD><TD><INPUT TYPE=CHECKBOX NAME=\"Rename[]\" VALUE=\"$Files\"><INPUT TYPE=TEXT NAME=\"NewName[$Files]\"></TD> </TR>\n"); } } closedir ($Open);

    Here is how I've looped through every file (and directory) located within the users directory. For these purposes, I only want to deal with files, not directories, so the is_ file() function is used to insure that files will be listed while directories will not be.

  10. Create the upload option in the form.

    print ("<TR><TD COLSPAN=4 ALIGN=CENTER>&nbsp;</TD></TR>\n"); print ("<TR><TD COLSPAN=4 ALIGN=CENTER><INPUT TYPE=CHECKBOX NAME=\"Upload\" VALUE=\"Yes\"> Upload a file to the server: <INPUT TYPE=FILE NAME=\"File\" SIZE=20></TD></TR>\n"); print ("<TR><TD COLSPAN=4 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=\"SUBMIT\" VALUE=\"Submit!\"> </FORM></TD></TR>\n");

  11. Close the PHP and the HTML.

    ?></TABLE></BODY></HTML>

  12. Save your script as files.php, upload it to the server (in the same location as the users directory), and test it in your Web browser (Figures 10.16 and 10.17).

    Figure 10.16. Upon first arriving at the page, this is what the user sees. Checkboxes are displayed giving the user of deleting, renaming, and uploading files.

    Figure 10.17. Here I've uploaded one new file, deleted another, and renamed a third. Each successful operation is reported and an update list of the directory's contents is displayed.

Tip

If you wanted, using the NewUser.html and HandleNewUser.php pages, files.php (with some modifications) could be copied into each new user's directory. Then, when the user logged in, they could be sent to their control panel wherein they could manage stored files.

Tip

To find out about other file and directory functions, look in the PHP manual under Directories and Filesystem .

I l @ ve RuBoard

Категории