PHP Tutorials - Herong's Tutorial Examples - v5.13, by Dr. Herong Yang
File Upload Web Form
This section provides a tutorial example on how to write Web form to upload files. type='file' attribute of the 'input' displays 'Choose File' button for user to select a local file to upload.
To manage file uploads with PHP scripts, you need to build a Web form for users to file, select and submit files from their local computers. Here are some guidelines on building file upload forms:
1. Using <form method="POST" ...> to ensure that uploaded files are included in the HTTP request body.
2. Using <form enctype="multipart/form-data" ...> to ensure that uploaded files are properly encoded as multiple attachments.
3. Using <input type="hidden" name="MAX_FILE_SIZE" value="nnnnn"/> before any <input type="file" .../> elements to limit the upload file size to the given number of bytes.
4. Using <input type="file" ...> to display a "Choose File" button to allow the user to search and select a local file to upload.
Here is an example of a Web form, file-upload-form.html, for file upload.
<html> <!-- file-upload-form.html - Copyright (c) HerongYang.com. All Rights Reserved. --> <body> <h4>File Upload Form</h4> <form action="file-upload-handler.php" method="POST" enctype="multipart/form-data"> <p>1st file to upload: <input type="file" name="file_one"/></p> <p>2nd file to upload: <input type="file" name="file_two"/></p> <p>Click to upload: <input type="submit" name="submit" value=Upload></p> </form> </body> </html>
Put the Web form file, file-upload-form.html, to the Apache Web server on the local computer. Then open this form with a Web browser using http://localhost/file-upload-form.html.
Click "Choose File" buttons to select two different files, Hello.php and dot.gif:
See the next tutorial on how to write file-upload-handler.php to received the uploaded files on the Web server.
Table of Contents