1.4. Creating a Form That Allows Customers to Upload a File

Sometimes you want to receive an entire file of information from a user, such as user résumés for your job-search Web site or pictures for your photo album Web site. Or, suppose you're building the catalog from information supplied by the Sales department. In addition to descriptive text about the product, you want Sales to provide a picture of the product. You can supply a form that Sales can use to upload an image file.

1.4.1. Using a form to upload the file

You can display a form that allows a user to upload a file by using an HTML form designed for that purpose. The general format of the form is as follows:

<form enctype="multipart/form-data"
          action="processfile.php" method="POST">
  <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
  <input type="file" name="user_file" />
  <input type="submit" value="Upload File" />
</form>

Notice the following points regarding the form:

  • The enctype attribute is used in the <form> tag. You must set this attribute to multipart/form-data when uploading a file to ensure that the file arrives correctly.

  • A hidden field is included that sends a value (in bytes) for MAX_FILE_SIZE. If the user tries to upload a file that is larger than this value, it won't upload. You can set this value as high as 2MB. If you need to upload a file larger than 2MB, you must change the default setting for upload_max_filesize in php.ini to a larger number before sending a value larger than 2MB for MAX_FILE_SIZE ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.