Page 164 - Open Soource Technologies 304.indd
P. 164
Unit 9: Working with Forms
Lines 12 through 14 create the $msg string, which contains the values typed by the user in the Notes
form fields. This string is the one sent in the email. Note the use of the concatenation operator
(.=) when adding to the variable $msg, in lines 13 and 14.
Lines 16 and 17 are hard-coded variables for the email recipient and the subject of the email
message. Replace you@yourdomain.com with your own email address, obviously. If you want
to change the subject, feel free! Lines 18 and 19 set up some mail headers, namely From: and
Reply-to: headers. You could put any value in the From: header; this is the information that
displays in the From or Sender column of your email application when you receive this mail.
The mail() function takes four parameters: the recipient, the subject, the message, and any
additional mail headers.
The order of these parameters is shown in line 21, and your script is complete after you close
up your PHP block and your HTML elements in lines 22-24.
9.8 Working with File Uploads
So far, we’ve looked at simple form input. However, all popular Web browsers support file
uploads, and so, of course, does PHP. In this section, you examine the features that PHP makes
available to deal with this kind of input. Information about the uploaded file becomes available
to you in the $_FILES superglobal, which is indexed by the name of the upload field (or fields)
in the form. The corresponding value for each of these keys is an associative array. These fields
are described in Table 9.1, using fileupload as the name of the form field used for the upload.
Table 9.1: File Upload Global Variables
Element Contains Example
$_FILES[‘fileupload’] [‘name’] Original name of uploaded file test.gif
$_FILES[‘fileupload’] [‘tmp_name’] Path to temporary file /tmp/phprDfZvN
$_FILES[‘fileupload’] [‘size’] Size (in bytes) of uploaded file 6835
MIME type of uploaded file
$_FILES[‘fileupload’] [‘type’] image/gif
(where given by client)
Keep these elements in the back of your mind for a moment, while we create
the upload form in the next section.
9.9 Creating the File Upload Form
First, we must create the HTML form to handle the upload. HTML forms that include file upload
fields must include an ENCTYPE argument:
ENCTYPE=”multipart/form-data”
PHP also works with an optional hidden field that can be inserted before the file upload field.
This field must be called MAX_FILE_SIZE and should have a value representing the maximum
size in bytes of the file that you’re willing to accept. This size cannot override the maximum size
LOVELY PROFESSIONAL UNIVERSITY 159