Specifications
You can choose whatever name you like for the file, but keep it in mind as you will use
this name to access your file from the receiving PHP script.
Writing the PHP to Deal with the File
Writing the PHP to catch the file is pretty straightforward.
When the file is uploaded, it will go into a temporary location on the Web server. This is the
Web server’s default temporary directory. If you do not move or rename the file before your
script finishes execution, it will be deleted.
Given that your HTML form has a field in it called
userfile, you will end up with four vari-
ables being passed to PHP:
• The value stored in
$userfile is where the file has been temporarily stored on the Web
server.
• The value stored in $userfile_name is the file’s name on the user’s system.
• The value stored in $userfile_size is the size of the file in bytes.
• The value stored in $userfile_type is the MIME type of the file, for example,
text/plain or image/gif.
You can also access these variables via the $HTTP_POST_FILES array, as follows:
• $HTTP_POST_FILES[‘userfile’][‘tmp_name’]
• $HTTP_POST_FILES[‘userfile’][‘name’]
• $HTTP_POST_FILES[‘userfile’][‘size’]
• $HTTP_POST_FILES[‘userfile’][‘type’]
Given that you know where the file is and what it’s called, you can now copy it to somewhere
useful. At the end of your script’s execution, the temporary file will be deleted. Hence, you
must move or rename the file if you want to keep it.
In our example, we’re going to use the uploaded files as recent news articles, so we’ll strip out
any tags that might be in them, and move them to a more useful directory. A script that does
this is shown in Listing 16.2.
LISTING 16.2 upload.php—PHP to Catch the Files from the HTML Form
<head>
<title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
Advanced PHP Techniques
P
ART IV
354
21 7842 CH16 3/6/01 3:40 PM Page 354










