MoMoMajor Posted July 25, 2009 Share Posted July 25, 2009 how can i get a variable from an if statement that is created and exists only in the if statement? Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/ Share on other sites More sharing options...
smerny Posted July 25, 2009 Share Posted July 25, 2009 if the statement isnt ran? can you show your code and explain more on what your trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882505 Share on other sites More sharing options...
MoMoMajor Posted July 25, 2009 Author Share Posted July 25, 2009 Ok so I have an image upload form that validates files. I want to get the value named $error_message out of the if statement so i can post it to the screen. I have to do this because the header script at the start of my script makes it so the errors that are in the die function don't display at all. I had it working with sessions but that quickly got scrapped because after a while the script would stop uploading pictures. if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) { $error_message = "file is too big" die("<BR><BR>Error: Upload file size too large: (<b>" . $_FILES['userfile']['size'] . "</b>). Must not exceed ".$max_size."kb."); } $array = explode(".", $_FILES['userfile']['name']); $nr = count($array); $ext = $array[$nr-1]; if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && ($ext !="pjpeg")) $error_message = "extension is wrong" die("<BR><BR>Error: file extension un-recognized. Be sure your image follows the correct extension (.JPG or .PNG)"); if(($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png")) { $error_message = "extension is wrong" die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG images allowed."); } if(($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) { $error_message = "extention is wrong" die("<BR><BR>Error: Upload file type un-recognized. Only .JPG or .PNG images allowed."); } Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882508 Share on other sites More sharing options...
vineld Posted July 25, 2009 Share Posted July 25, 2009 I'm not sure what you wish to do with the variable but everything within the if block is available outside the if block as well so you may do whatever you wish with the error message. Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882512 Share on other sites More sharing options...
MoMoMajor Posted July 25, 2009 Author Share Posted July 25, 2009 I'm not sure what you wish to do with the variable but everything within the if block is available outside the if block as well so you may do whatever you wish with the error message. but when I echo $error_message it does not return anything. it is just left blank even when i upload a gif or a file that is too large. Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882514 Share on other sites More sharing options...
Philip Posted July 25, 2009 Share Posted July 25, 2009 but when I echo $error_message it does not return anything. it is just left blank even when i upload a gif or a file that is too large. Probably because the script is told to die right after you set $error_message Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882515 Share on other sites More sharing options...
MoMoMajor Posted July 25, 2009 Author Share Posted July 25, 2009 but when I echo $error_message it does not return anything. it is just left blank even when i upload a gif or a file that is too large. Probably because the script is told to die right after you set $error_message If I was to remove the die() it would make it so people can upload anything to the server. Is there anyway I can make it so I can keep the die but still return the error message on a case by case bases? Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882520 Share on other sites More sharing options...
MoMoMajor Posted July 25, 2009 Author Share Posted July 25, 2009 funnily enough even with the die commands removed it still does not print out the message It's been messed up ever since I redirected the page with header: index.php and i only did that because the form kept submitting the same image to the server when it was refreshed. Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882524 Share on other sites More sharing options...
Philip Posted July 25, 2009 Share Posted July 25, 2009 Something like: <?php $error_message = array(); if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) { $error_message['size'] = "Upload file size too large: (<b>{$_FILES['userfile']['size']}</b>). Must not exceed {$max_size}kb."; } $array = explode(".", $_FILES['userfile']['name']); $nr = count($array); $ext = $array[$nr-1]; // I condensed all 3 of the next if's into one... since they were checking for the same thing pretty much. if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && ($ext !="pjpeg") && ($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png") && ($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) { $error_message['format'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed."; } if(count($error_message)>0) { // we had errors: foreach($error_message as $message) { echo '<strong>ERROR:</strong> '.$message.'<br>'; } // die, exit, or show form again. whatever you want here. } else { // we didn't have errors, continue with uploading // ... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882525 Share on other sites More sharing options...
MoMoMajor Posted July 25, 2009 Author Share Posted July 25, 2009 Something like: <?php $error_message = array(); if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) { $error_message['size'] = "Upload file size too large: (<b>{$_FILES['userfile']['size']}</b>). Must not exceed {$max_size}kb."; } $array = explode(".", $_FILES['userfile']['name']); $nr = count($array); $ext = $array[$nr-1]; // I condensed all 3 of the next if's into one... since they were checking for the same thing pretty much. if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && ($ext !="pjpeg") && ($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png") && ($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) { $error_message['format'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed."; } if(count($error_message)>0) { // we had errors: foreach($error_message as $message) { echo '<strong>ERROR:</strong> '.$message.'<br>'; } // die, exit, or show form again. whatever you want here. } else { // we didn't have errors, continue with uploading // ... } ?> thanks for taking the time to write this up. Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882528 Share on other sites More sharing options...
MoMoMajor Posted July 25, 2009 Author Share Posted July 25, 2009 Something like: <?php $error_message = array(); if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) { $error_message['size'] = "Upload file size too large: (<b>{$_FILES['userfile']['size']}</b>). Must not exceed {$max_size}kb."; } $array = explode(".", $_FILES['userfile']['name']); $nr = count($array); $ext = $array[$nr-1]; // I condensed all 3 of the next if's into one... since they were checking for the same thing pretty much. if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && ($ext !="pjpeg") && ($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png") && ($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) { $error_message['format'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed."; } if(count($error_message)>0) { // we had errors: foreach($error_message as $message) { echo '<strong>ERROR:</strong> '.$message.'<br>'; } // die, exit, or show form again. whatever you want here. } else { // we didn't have errors, continue with uploading // ... } ?> thanks for taking the time to write this up Quote Link to comment https://forums.phpfreaks.com/topic/167361-getting-information-back-from-if-statements/#findComment-882529 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.