Sometimes you want to give users the ability to upload files in addition to standard form data. Because files are not sent in the same way as other form data, you must specify a particular type of encoding—
<form action="upload.php" method="POST" enctype="multipart/form-data">
An HTTP request that includes both regular form data and files has a special format, and this
enctype attribute is necessary for the browser's compliance.
The form element you use to allow the user to select a file for upload is very simple:
<input type="file" name="attachment" />
The rendering of this form element varies from browser to browser. Traditionally, the interface includes a standard text field as well as a browse button, so that the user can either enter the path to the file manually or browse for it. In Safari, only the browse option is available. Luckily, the behavior from a developer's perspective is the same.
To better illustrate the mechanics of a file upload, here's an example form that allows a user to upload an attachment:
<form action="upload.php" method="POST" enctype="multipart/form-data"> <p>Please choose a file to upload: <input type="hidden" name="MAX_FILE_SIZE" value="1024" /> <input type="file" name="attachment" /><br /> <input type="submit" value="Upload Attachment" /></p> </form>
The hidden form variable
MAX_FILE_SIZE indicates the maximum file size (in bytes) that the browser should allow. As with any client-side restriction, this is easily ...