Ryan R Posted May 27, 2011 Share Posted May 27, 2011 Hi All, Thank you very much for your immense help in advance. I have a question about "uploading a file" in Chapter 10. For handling error messages, this book used switch function. But I don't know where I can find defining codes in this web page. If an error occurs, how do you know the error falls into case 1, or case 2 or case 3 or .. ? Here below are the codes from the book. ---------------------------------------------------------------------------------------------------------------------------------------- <!DOCTYPE html> <html dir="ltr" lang="en-US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Upload an Image</title> <style type="text/css" title="text/css" media="all"> .error{ font-weight:bold; color:#c00; } </style> </head> <body> <?php #script 10.3 - upload_image.php //check if the form has been submitted if(isset($_POST['submitted'])) { //check for an uploaded file if(isset($_FILES['upload'])) { //validate the type. should be JPEG or PNG $allowed = array('image/pjpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png','images/pjpeg','images/jpeg','images/JPG','images/X-PNG','images/PNG','images/png','images/x-png' ); if(in_array($_FILES['upload']['type'], $allowed)) { //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}")) { echo'<p><em>The file has been uploaded!</em></p>'; } // end of move .. IF }else{ //Invalid type echo'<p class="error">Please upload a JPEG or PNG image.</p>'; } }// end of isset($_FILES['upload']) IF //check for an error if($_FILES['upload']['error'] > 0) { echo'<p class="error">The file could not be uploaded because: <strong>'; //print the message based upon the error switch($_FILES['upload']['error']) { case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.'; break; case 3: print 'The file was only partially uploaded.'; break; case 4: print 'No file was uploaded.'; break; case 6: print 'No temporary folder was available'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occured.'; break; } // end of swtich print'</strong></p>'; }//end of error IF //delete the temporary file if it still exists; if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) { unlink($_FILES['upload']['tmp_name']); } }// end of the submitted conditional ?> <form enctype="multipart/form-data" action="upload_image.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="524288"> <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend> <p><b>File:</b> <input type="file" name="upload" /></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"></div> <input type="hidden" name="submitted" value="TRUE" /> </form> </body> </html> ---------------------------------------------------------------------------------------------------------------- Thanks again in advance for your help and look forward to hearing from you soon. Link to comment Share on other sites More sharing options...
bahaa Posted May 27, 2011 Share Posted May 27, 2011 These numbers are built in constants, and each number has it is own definition. hopefully I answered your question. have a look at this: http://php.net/manual/en/features.file-upload.errors.php 1 Link to comment Share on other sites More sharing options...
Ryan R Posted May 27, 2011 Author Share Posted May 27, 2011 This is what I am looking for. Thank you Bahaa! Link to comment Share on other sites More sharing options...
Recommended Posts