PHP3 Manual
PrevChapter 2. PHP3 featuresNext

File upload support

PHP is capable of receiving file uploads from any RFC-1867 compliant browser. This feature lets people upload both text and binary files. With PHP's authetication and logical functions, you have full control over who is allowed to upload and what is to be done with the file once it has been uploaded.

A file upload screen can be built by creating a special form which looks something like this:

Example 2-3. File Upload Form

    <FORM ENCTYPE="multipart/form-data" ACTION="_URL_" METHOD=POST>
    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000">
    Send this file: <INPUT NAME="userfile" TYPE="file">
    <INPUT TYPE="submit" VALUE="Send File">
    </FORM>
The _URL_ should point to a php html file. The MAX_FILE_SIZE hidden field must precede the file input field and its value is the maximum filesize accepted. The value is in bytes. In this destination file, the following variables will be defined upon a successful upload:

Note that the "$userfile" part of the above variables is whatever the name of the INPUT field of TYPE=file is in the upload form. In the above upload form example, we chose to call it "userfile".

Files will by default be stored in the server's default temporary directory. This can be changed by setting the environment variable TMPDIR in the environment in which PHP runs. Setting it using a PutEnv() call from within a PHP script will not work though.

The PHP script which receives the uploaded file should implement whatever logic is necessary for determining what should be done with the uploaded file. You can for example use the $file_size variable to throw away any files that are either too small or too big. You could use the $file_type variable to throw away any files that didn't match a certain type criteria. Whatever the logic, you should either delete the file from the temporary directory or move it elsewhere.

Please note that the CERN httpd seems to strip off everything starting at the first whitespace in the content-type mime header it gets from the client. As long as this is the case, CERN httpd will not support the file upload feature.


PrevHomeNext
GIF creation with PHPUpHTTP cookie support