richiejones24 Posted July 27, 2011 Share Posted July 27, 2011 Total Nood to PHP trying to write a multiple image upload script, however having problem as the image validation kills the script and it wont upload any ideas? $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); //These will be the types of file that will pass the validation. $upload_path = '../pic_upload/'; // The place the files will be uploaded to. foreach ($_FILES["pictures"]["error"] as $key => $error) { $filename = $_FILES['pictures']['name'][$key]; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); if ($_FILES["pictures"]["size"][$key] >= 700000) { //Check the Size die ('size to big'); } if (empty($filename)) { //Is the field empty die ('one of the fields is empty but the rest have been upload'); } if(!in_array($ext,$allowed_filetypes)) //Is the file Allowed die('The file you attempted to upload is not allowed.'); if ($error == UPLOAD_ERR_OK) { //Upload File check for errors echo"$error_codes[$error]"; move_uploaded_file($_FILES["pictures"]["tmp_name"][$key],$upload_path . $_FILES["pictures"]["name"][$key]) or die("Problems with upload"); } MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/ Share on other sites More sharing options...
AyKay47 Posted July 27, 2011 Share Posted July 27, 2011 what exactly is your problem? what errors do you receive Quote Link to comment https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/#findComment-1248101 Share on other sites More sharing options...
PFMaBiSmAd Posted July 27, 2011 Share Posted July 27, 2011 The following is the basis for the code you are using (gotten from the php.net documentation.) You would put your application level validation logic inside the if(){...} statement, because you can only access and check the uploaded file information when the file was actually uploaded without any errors - <?php foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); } } ?> The code you have now makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/#findComment-1248153 Share on other sites More sharing options...
Pikachu2000 Posted July 27, 2011 Share Posted July 27, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/#findComment-1248154 Share on other sites More sharing options...
richiejones24 Posted July 28, 2011 Author Share Posted July 28, 2011 The following is the basis for the code you are using (gotten from the php.net documentation.) You would put your application level validation logic inside the if(){...} statement, because you can only access and check the uploaded file information when the file was actually uploaded without any errors - <?php foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); } } ?> The code you have now makes no sense. Totally Confused now :-s The code works and uploads the files ok however, the script is for multiple file upload and if the user uploads a file to form field 3 for example and form field 1 is left empty the script dies and no files are uploaded i belive it is this part that is killing it if (empty($filename)) { //Is the field empty die ('one of the fields is empty but the rest have been upload'); } any ideas how to get round this? Quote Link to comment https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/#findComment-1248373 Share on other sites More sharing options...
AyKay47 Posted July 28, 2011 Share Posted July 28, 2011 since you have 3 different file input fields you will need to assign them each a unique name and check if one of them has a value in order for your script to run Quote Link to comment https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/#findComment-1248502 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2011 Share Posted July 28, 2011 the script dies Of course, because you are using the die() statement to display validation errors and your code will die on the first error that is found. To validate multiple pieces of user data, you should use an array to both hold validation error messages and to serve as a flag that errors have occurred. <?php $errors = array(); // define an array to hold errors if(some_validation_condition_is_not_met){ $errors[] = "user message you want to display for this error"; } // check if there were any validation errors - if(empty($errors)){ // no errors, use the data here... } You can loop over the $errors array to display any errors that have occurred. Quote Link to comment https://forums.phpfreaks.com/topic/243002-file-upload-problems-noob/#findComment-1248510 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.