Love2c0de Posted June 22, 2012 Share Posted June 22, 2012 Just testing out a simple error reporting thing with my code because it only seems to execute when $_FILES['myfile']['error'] == 0. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" /> <title>PHP Exercises</title> </head> <body> <?php $form = "<form action='examples.php' method='POST' enctype='multipart/form-data'> <table> <tr> <th>Browse:</th> <td><input type='file' name='myfile' /><input type='hidden' name='MAX_FILE_SIZE' value='7340032' /></td> </tr> <tr> <th>Upload:</th> <td><input type='submit' name='submitbutton' value='Submit' /></td> </tr> </table> </form>"; echo "$form"; function processFile(){ } if(isset($_POST['submitbutton'])){ $errorIndex = $_FILES['myfile']['error']; if ($errorIndex == 0){ echo 'no errors.'; } else{ die("There was an error with the upload."); } } ?> </body> </html> Does anyone know why it isn't printing my error message when it is returning something other than 0? For instance, when I upload a file which is larger in size than my MAX_FILE_SIZE input tag, it doesn't return any error.... I believe it should return: UPLOAD_ERR_FORM_SIZE but it doesn't execute the error message.... Regards, LC. Quote Link to comment https://forums.phpfreaks.com/topic/264603-error-reporting-with-file-uploads/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 22, 2012 Share Posted June 22, 2012 Probably because the size of the file also exceeded the post_max_size setting, which causes both the $_POST and $_FILES arrays to be empty - post_max_size integer Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. <form action="edit.php?processed=1">, and then checking if $_GET['processed'] is set. For upload form processing code, you need to test if the $_POST/$_FILES array are not empty, before you test any values in those arrays. If the $_POST array is empty, your if(isset($_POST['submitbutton'])){ statement will always be false. To detect if an upload form has been submitted, we typically suggest using the following logic to test if a form has been submitted - if($_SERVER['REQUEST_METHOD'] == 'POST'){ // a post mode form has been submitted, test if the $_FILES array is not empty and that the ['error'] element is zero // you could also test if the ['error'] element is set and is equal to zero or you can test if the ['error'] element is exactly (===) equal to zero } Quote Link to comment https://forums.phpfreaks.com/topic/264603-error-reporting-with-file-uploads/#findComment-1356089 Share on other sites More sharing options...
conan318 Posted June 22, 2012 Share Posted June 22, 2012 try this 200kb upload limit. just adjust the upload limit to suite your needs. $errorIndex = $_FILES['myfile']['size']; if ($errorIndex <= 200000){ echo 'no errors.'; } elseif ($errorIndex != 0){ die("No file was selected."); }else{ die("upload file is to large."); } Quote Link to comment https://forums.phpfreaks.com/topic/264603-error-reporting-with-file-uploads/#findComment-1356093 Share on other sites More sharing options...
Love2c0de Posted June 22, 2012 Author Share Posted June 22, 2012 Thanks for all the info, I am going to get to work right now. Btw, I am trying to upload files which are 10mb or less. Kind regards, BuNgLe. Quote Link to comment https://forums.phpfreaks.com/topic/264603-error-reporting-with-file-uploads/#findComment-1356297 Share on other sites More sharing options...
Love2c0de Posted June 22, 2012 Author Share Posted June 22, 2012 Hi guys, am I going in the right direction? function getFile(array $filesarray){ $name = $_FILES['myfile']['name']; $type = $_FILES['myfile']['type']; $size = $_FILES['myfile']['size']; $tmpname = $_FILES['myfile']['tmp_name']; } if($_SERVER['REQUEST_METHOD'] == 'POST'){ if($_FILES['myfile']['error'] == 0){ getFile($_FILES); } else{ die('No file selected.'); } } else{ header("Location: examples.php"); } Regards. BuNgLe Quote Link to comment https://forums.phpfreaks.com/topic/264603-error-reporting-with-file-uploads/#findComment-1356300 Share on other sites More sharing options...
Love2c0de Posted June 23, 2012 Author Share Posted June 23, 2012 Updated Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" /> <title>PHP Exercises</title> </head> <body> <?php $form = "<form action='examples.php' method='POST' enctype='multipart/form-data'> <table> <tr> <th>Browse:</th> <td><input type='file' name='myfile' /><input type='hidden' name='MAX_FILE_SIZE' value='7340032' /></td> </tr> <tr> <th>Upload:</th> <td><input type='submit' name='submitbutton' value='Submit' /></td> </tr> </table> </form>"; echo "$form"; function getFile(array $file){ $mfs = $_POST['MAX_FILE_SIZE']; $name = $file['myfile']['name']; $type = $file['myfile']['type']; $size = $file['myfile']['size']; $tmpname = $file['myfile']['tmp_name']; $date = date('d/m/Y'); $ext = substr($name, strrpos($name, '.')); if($name){ if($ext == '.dem'){ if($size <= $mfs){ if(move_uploaded_file($tmpname, "files/"."$name")){ echo "File Uploaded Ok!"; //insert data to database here...? } } else{ die("Error: Filesize too large. You can only upload files which are 10mb or less."); } } else{ die("Error: You cannot upload that file type. Demo files only."); } } else{ echo "$form"; } } if($_SERVER['REQUEST_METHOD'] == 'POST'){ if($_FILES['myfile']['error'] == 0){ getFile($_FILES); } else{ die('Did not receive a file.'); } } ?> </body> </html> Is this code ok or have I missed something? regards, BuNgLe Quote Link to comment https://forums.phpfreaks.com/topic/264603-error-reporting-with-file-uploads/#findComment-1356381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.