COM372: Notes, Chapter 18

Interacting with the File System and the Server
pages 401-418

Syllabus | Grading | Reading Notes | Dr. Logan

Uploading Files: You can send a local file to the server using a form. The listing 18.1 shows how to do this, and this is repeated here:
<html><head><title>Form Upload</title></head>
<body>
<h1>Upload a file<h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload this file:<input type="file" name="userfile">
<input type="submit" value="upload file">
</form></body></html>

Note the use of POST, the attribute enctype, the hidden field to send the maximum file size, and the input of type file.

Is it obvious that you should restrict file uploads to trusted administrators?

PHP to catch the file (in PHP 5) is listed in Listing 18.2. Uploaded files go to a temporary internet file storage area and are deleted if you do not do something with them. Files are handled using data stored in the superglobal $_FILES array. For a file passed with the form input "userfile," $_FILES will contain this data:

Listing 18.2 is a script to recieve this file and file it on the server. This script includes means to specify where the file will go
$upfile='/my_file_directory/'.$_FILES['myuserfile']['myfilename'];
as well as error control in case the file cannot be moved from temporary storage to the specified directory, and a mechanism for echoing file content. Error checking on the upload generates codes, listing on p. 406, with associated constants, as
Be careful and paranoid here, taking steps such as authenticating users, limiting permissions to trusted users, rewriting file names, placing files outside of the web root directory, etc. (p. 407-8).

Directory functions: PHP directory and file system functions help users see and manipulate uploaded files.

Interacting with the File System: Once you have information about directories, you can also get information about files. Listing 18.3 includes a loop to read files names
while ($file = readdir($dir))
{
echo '<a href="filedetails.php?file='.$file.'">'.$file.'</a><br />';
}

Listing 18.4 breaks into this loop and generates several pieces of information about individual files, using functions including

and several others, with the caveate that some of the functions are not supported under windows OS (p. 411-414). These properties can be changes in unix environments, but not in windows (p. 414); files may also be created, moved, or deleted using touch() to create or alter time last modified, unlink () to delete, or copy() and rename() to move files. In windows, system ("del filename.ext"); replaces unlink () (p. 414).

Server Command Execution Functions: You can execute command-line instructions to the server

These are illustrated in listing 18.5 (p. 416-7).

Server environment variables: The function phpinfo(); generates a list of all PHP environment variables. Individual variables can be obtained using, for example getenv("variable_name"); these can also be set using putenv().