Stream Me Up, Scotty (part 2) - Upsa-daisy! (
Page 4 of 6 )
PHP also
allows you to upload files via HTTP, assuming you have an RFC-1867 compliant
browser (if you're using a relatively recent version of either Internet Explorer
or Netscape Navigator, you're OK). There are two components to this: the form
which accepts the file upload, and the server-side script which checks the file
and decides where to put it.
Let's start with the form first.
<html>
<head>
</head>
<body>
<center>
<form enctype="multipart/form-data"
action="upload.php4" method=post>
Enter file name <input name="upfile" type="file">
<br>
<input type="submit" value="Beam Me Up, Scotty">
</form>
</center>
</body>
</html>
There are two points of note here: the form's encoding type
of "multipart/form-data", and the file browse button <input name="upfile"
type="file">, which displays a directory browser and lets you select a file
from your system. Once you're done, hit the Submit button and let the script
"upload.php4" take over.
Before I take you through "upload.php4", though,
you should be aware that when a file is uploaded in this manner, PHP typically
creates four variables, each of them containing the name of the file field as
prefix. In the example above, these variables would be:
$upfile_name -
the original name of the file
$upfile - the temporary name assigned to
the file by PHP once it has been successfully uploaded
$upfile_size - the
size of the file
$upfile_type - the MIME file type
You should also
note that once the file is uploaded, it is stored in the temporary file area on
the remote computer, and your script needs to move it out of there to your
desired destination directory. If this does not happen, the file will be
automatically deleted.
As stated in the first part of this article, these
variables are also available in the $HTTP_POST_FILES array, and it is
recommended that you use this array to access the variables above (rather than
accessing them directly) for greater security.
This article copyright Melonfire 2000. All rights
reserved.