11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 15 • HANDLING FILE UPLOADSwww.it-ebooks.infowill be returned. Because of this, you can forgo using is_uploaded_file() as a precursor condition tousing move_uploaded_file().Using move_uploaded_file() is simple. Consider a scenario in which you want to move the uploadedclass notes file to the directory /www/htdocs/classnotes/ while also preserving the file name as specifiedon the client:move_uploaded_file($_FILES['classnotes']['tmp_name'],"/www/htdocs/classnotes/".$_FILES['classnotes']['name']);Of course, you can rename the file to anything you wish after it’s been moved. It’s important,however, that you properly reference the file’s temporary name within the first (source) parameter.Upload Error MessagesLike any other application component involving user interaction, you need a means to assess theoutcome, successful or otherwise. How do you know with certainty that the file-upload procedure wassuccessful? And if something goes awry during the upload process, how do you know what caused theerror? Happily, sufficient information for determining the outcome (and in the case of an error, thereason for the error) is provided in $_FILES['userfile']['error']:• UPLOAD_ERR_OK: A value of 0 is returned if the upload is successful.• UPLOAD_ERR_INI_SIZE: A value of 1 is returned if there is an attempt to upload a filewhose size exceeds the value specified by the upload_max_filesize directive.• UPLOAD_ERR_FORM_SIZE: A value of 2 is returned if there is an attempt to upload afile whose size exceeds the value of the max_file_size directive, which can beembedded into the HTML form■ Note Because the max_file_size directive is embedded within the HTML form, it can easily be modified by anenterprising attacker. Therefore, always use <strong>PHP</strong>’s server-side settings (upload_max_filesize,post_max_filesize) to ensure that such predetermined absolutes are not surpassed.• UPLOAD_ERR_PARTIAL: A value of 3 is returned if a file is not completely uploaded.This might happen if a <strong>net</strong>work error causes a disruption of the upload process.• UPLOAD_ERR_NO_FILE: A value of 4 is returned if the user submits the form withoutspecifying a file for upload.• UPLOAD_ERR_NO_TMP_DIR: A value of 6 is returned if the temporary directory doesnot exist.• UPLOAD_ERR_CANT_WRITE: Introduced in <strong>PHP</strong> 5.1.0, a value of 7 is returned if the filecan’t be written to the disk.• UPLOAD_ERR_EXTENSION: Introduced in <strong>PHP</strong> 5.2.0, a value of 8 is returned if an issuewith <strong>PHP</strong>’s configuration caused the upload to fail.324

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

Saved successfully!

Ooh no, something went wrong!