Page 295 - Beginning PHP 5.3
P. 295

Chapter 9: Handling HTML Forms with PHP
                           Creating File Upload Forms

                           As well as sending textual data to the server, Web forms can be used to upload files to the server. If
                         you ’ ve used a Web - based email service such as Yahoo! Mail or Gmail, chances are you ’ ve sent email with
                         attachments. To add an attachment, you generally click the Browse button in the Web page to select a file
                         on your computer. Then, when you submit the form, your browser sends the file to the server along with
                         the other form data.
                           You ’ ve already seen how to create a file select field at the start of this chapter:


                               < label for=”fileSelectField” >A file select field < /label >


                             < input type=”file” name=”fileSelectField” id=”fileSelectField” value=”” />

                           In addition, a form containing a file select field must use the  post  method, and it must also have an
                            enctype=”multipart/form - data”  attribute in its  <form>  tag, as follows:
                               < form action=”form_handler.php” method=”post” enctype=”multipart/form-data” >

                           This attribute ensures that the form data is encoded as mulitpart MIME data  —  the same format that ’ s
                          used for encoding file attachments in email messages  —  which is required for uploading binary data
                          such as files.

                               You can have as many file select fields as you like within your form, allowing your users to upload
                             multiple files at once.

                           Accessing Information on Uploaded Files

                           Once the form data hits the server, the PHP engine recognizes that the form contains an uploaded file or
                         files, and creates a superglobal array called   $_FILES  containing various pieces of information about the
                         file or files. Each file is described by an element in the   $_FILES  array keyed on the name of the field that
                          was used to upload the file.

                            For example, say your form contained a file select field called   photo :


                               < input type=”file” name=”photo” value=”” />
                           If the user uploaded a file using this field, its details would be accessible via the following PHP
                         array element:

                             $_FILES[“photo”]

                           This array element is itself an associative array that contains information about the file. For example, you
                         can find out the uploaded file ’ s filename like this:

                             $filename = $_FILES[“photo”][“name”];








                                                                                                         257





          c09.indd   257                                                                              9/21/09   7:23:47 PM
                                                                                                      9/21/09   7:23:47 PM
          c09.indd   257
   290   291   292   293   294   295   296   297   298   299   300