13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Implementing Administrative Functions<br />

731<br />

Listing 30.16<br />

Continued<br />

do_html_footer();<br />

exit;<br />

}<br />

move_uploaded_file($_FILES['userfile']['tmp_name'][$i],<br />

$destination);<br />

}<br />

$i++;<br />

display_preview_button($list, $mailid, 'preview-html');<br />

display_preview_button($list, $mailid, 'preview-text');<br />

display_button('send', "&id=$mailid");<br />

echo "&nbsp;";<br />

do_html_footer();<br />

?><br />

Let’s walk through the steps in Listing 30.16. First, you start a session <strong>and</strong> check that the<br />

user is logged in as an administrator; you don’t want to let anybody else upload files.<br />

Strictly speaking, you should probably also check the list <strong>and</strong> mailid variables for<br />

unwanted characters, but we ignored this for the sake of brevity.<br />

Next, you set up <strong>and</strong> send the headers for the page <strong>and</strong> validate that the form was<br />

filled in correctly.This step is important here because it’s quite a complex form for the<br />

user to fill out.<br />

Then you create an entry for this mail in the database <strong>and</strong> set up a directory in the<br />

archive for the mail to be stored in.<br />

Next comes the main part of the script, which checks <strong>and</strong> moves each of the<br />

uploaded files.This is the part that is different when uploading multiple files.You now<br />

have four arrays to deal with; these arrays are called $_FILES[‘userfile’][‘name’],<br />

$_FILES[‘userfile’][‘tmp_name’], $_FILES[‘userfile’][‘size’], <strong>and</strong><br />

$_FILES[‘userfile’][‘type’].They correspond to their similarly named<br />

equivalents in a single file upload, except that each of them is an array.The first<br />

file in the form is detailed in $_FILES[‘userfile’][‘tmp_name’][0],<br />

$_FILES[‘userfile’][‘name’][0], $_FILES[‘userfile’][‘size’][0], <strong>and</strong><br />

$_FILES[‘userfile’][‘type’][0].<br />

Given these three arrays, you perform the usual safety checks <strong>and</strong> move the files into<br />

the archive.<br />

Finally, you give the administrator some buttons that she can use to preview the<br />

newsletter she has uploaded before she sends it <strong>and</strong> a button to send it.You can see the<br />

output from upload.php in Figure 30.13.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!