ZEND PHP 5 Certification STUDY GUIDE
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Web Programming ” 103<br />
in the example above). It is up to the script to move the file to a safe location if it so<br />
chooses—the temporary copy is automatically destroyed when the script ends.<br />
Inside your script, uploaded files will appear in the $_FILES superglobal array. Each<br />
element of this array will have a key corresponding to the name of the HTML element<br />
that uploaded a file (filedata in our case). The element will, itself, be an array with<br />
the following elements:<br />
name<br />
type<br />
size<br />
tmp_name<br />
error<br />
The original name of the file<br />
The MIME type of the file provided by the browser<br />
The size (in bytes) of the file<br />
The name of the file’s temporary location<br />
The error code associated with this file. A value of<br />
UPLOAD_ERR_OK indicates a successful transfer, while any other<br />
error indicates that something went wrong (for example, the<br />
file was bigger than the maximum allowed size).<br />
The real problem with file uploads is that most—but not all—of the information that<br />
ends up in $_FILES can be spoofed by submitting malicious information as part of the<br />
HTTP transaction. <strong>PHP</strong> provides some facilities that allow you to determine whether<br />
a file upload is legit. One of them is checking that the error element of your file<br />
upload information array is set to UPLOAD_ERR_OK. You should also check that size is<br />
not zero and that tmp_name is not set to none.<br />
Finally, you can use is_uploaded_file() to determine that a would-be hacker<br />
hasn’t somehow managed to trick <strong>PHP</strong> into building a temporary file name that, in<br />
reality, points to a different location, and move_uploaded_file() to move an uploaded<br />
file to a different location (a call to the latter function also checks whether the source<br />
file is a valid upload file, so there is no need to call is_uploaded_file() first):<br />
One of the most common mistakes that developers make when dealing with uploaded<br />
files is using the name element of the file data array as the destination when<br />
moving it from its temporary location. Because this piece of information is passed<br />
by the client, doing so opens up a potentially catastrophic security problem in your<br />
code. You should, instead, either generate your own file names, or make sure that<br />
you filter the input data properly before using it (this is discussed in greater detail in<br />
the Security chapter).<br />
Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)