shadiadiph Posted May 11, 2009 Share Posted May 11, 2009 I have a page which can submit up to 4 files I am checking to see if they have been uploaded using the following but keep getting the error Array to string conversion in if (is_uploaded_file($_FILES['fileatt']['name'])) { $fileatt_name = $_FILES['fileatt']['name']; $attachments1 = stripslashes($fileatt_name[0]); $attachments2 = stripslashes($fileatt_name[1]); $attachments3 = stripslashes($fileatt_name[2]); $attachments4 = stripslashes($fileatt_name[3]); } else { $error ="No files were uploaded"; } why am i getting the error? Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/ Share on other sites More sharing options...
gevans Posted May 11, 2009 Share Posted May 11, 2009 If you're uplading up to four files you need to be using a loop to check all four files, is_uploaded_file() needs to be called for each file Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831528 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 sorry not sure how to do this i need to checK first if the files have been uploaded and if they are below the MAX_FILE_SIZE if they are ok do an sql insert then create directories to save them to then upload the files to the directories i know how to do the later its just checking the file part i am not sure about i tried if ($_FILES["fileatt"]["error"] == UPLOAD_ERR_OK && $_FILES["fileatt"]["size"] >0) { // take action } else { // display error } but it didn't work Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831535 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2009 Share Posted May 11, 2009 What is the name of the uploaded files? Is it an array (which it should be as that would allow you to use a loop) or individual names? What is your form code? Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831539 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 <input type="file" name="fileatt[]" /> Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831540 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2009 Share Posted May 11, 2009 Example #3 in the php manual shows how to iterate over an array of uploaded files - http://www.php.net/manual/en/features.file-upload.post-method.php Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831545 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 thanks i shall read it all again for the 20th time lol problem with the upload error is the don't have one for if the MAX_FILE_SIZE is ok only if it isn't they have an error for if the file has uploaded its just one of those things they haven't thought of for example if this worked if ($_FILES["fileatt"]["error"] == UPLOAD_ERR_OK && $_FILES["fileatt"]["size"] >0) { i would not have a problem it would be easy Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831547 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2009 Share Posted May 11, 2009 There is an error if MAX_FILE_SIZE is exceeded - http://www.php.net/manual/en/features.file-upload.errors.php Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831553 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 thats true but there isnt one for if it isn't exceeded its ok i am getting somewhere think i might have solved soon but not sure i need to get some sleep first thanks for the input guys Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831571 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2009 Share Posted May 11, 2009 Think about it. If the upload was successful, without any errors, then the MAX_FILE_SIZE was not exceeded Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831578 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 thanks that makes it clearerish so if i was to use something like this foreach ($_FILES["fileatt"]["error"] as $key => $error) { if ($error ==UPLOAD_ERR_OK) { UPLOAD_ERR_OK Value: 0; There is no error, the file uploaded with success. does this mean that the file size is ok too or just that a file was uploaded doesn't say if its size is ok doesn't clearly state this in the manual if i the answer is the filesize is ok i would have had this finished hours ago. sorry i am just too tired 18 hours already today think i'll get some sleep and start again in about 4-5 hours Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831588 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 mm now i am having the problem the error message is always retuning $error=="4" foreach ($_FILES["fileatt"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $fileatt_name = $_FILES['fileatt']['name']; $attachments1 = stripslashes($fileatt_name[0]); $attachments2 = stripslashes($fileatt_name[1]); $attachments3 = stripslashes($fileatt_name[2]); $attachments4 = stripslashes($fileatt_name[3]); } if ($error=="4") { $action="reply"; $sideaction=""; $error ="You did not upload any file attachment do you still wish to proceed?"; } if ($error=="2") { $action="reply"; $sideaction="addattachments"; $error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again"; } } I only want to get the value of the first $error passed i tried $error[0] but it didn't work? Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831667 Share on other sites More sharing options...
mikr Posted May 11, 2009 Share Posted May 11, 2009 This may not solve your problem, but it might help move you forward. You are executing your first if statement for every file you attempt to upload. So, if one file out of all fails, your code will sort of act as though they all failed... Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831730 Share on other sites More sharing options...
mikr Posted May 11, 2009 Share Posted May 11, 2009 Also, in response to: I only want to get the value of the first $error passed i tried $error[0] but it didn't work? In your foreach loop, $error is returning an element from the array: $_FILES["fileatt"]["error"] (not the array itself), so if you want the first error, then you will want to check $_FILES["fileatt"]["error"][0]. Or, you could just write $error = $_FILES["fileatt"]["error"];, and then check $error[0]. Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831734 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 Thanks for trying but i just tried this did not upload any files should have returned error 4 but it returned the error value of ==2 if ($_FILES["fileatt"]["error"][0]==4); { $action="reply"; $sideaction=""; $error ="You did not upload any file attachment do you still wish to proceed?"; } if ($_FILES["fileatt"]["error"][0]==2); { $action="reply"; $sideaction="addattachments"; $error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again"; } Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831750 Share on other sites More sharing options...
mikr Posted May 11, 2009 Share Posted May 11, 2009 Are you sure? The code you posted has semicolons at the end of the if lines, which means any code within the brackets ({}) will be executed anyways. So, that code will always result in the $error message resulting in "An error occurred, you can only upload files..." Now, I am at a loss if that is just a mistake in inputting to the website here... Hope that helps. Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831796 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 i saw that and took the ; out had the same problem i have now taken out the for each loop at the start almost have it working [with code] if ($_FILES["fileatt"]["error"] == 0) { $fileatt_name = $_FILES['fileatt']['name']; $attachments1 = stripslashes($fileatt_name[0]); $attachments2 = stripslashes($fileatt_name[1]); $attachments3 = stripslashes($fileatt_name[2]); $attachments4 = stripslashes($fileatt_name[3]); } else { $error = ($_FILES["fileatt"]["error"]); for ($i=0; $i<count($error); $i++) { if ($error[$i] == 4) { $action="reply"; $sideaction=""; $error ="You did not upload any file attachment do you still wish to proceed?"; } if ($error[$i] == 2) { $action="reply"; $sideaction="addattachments"; $error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again"; } } } works fine but if the user uploads no file in attachment 1 and uploads one in attachment2 it returns the error of no file uploaded Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831810 Share on other sites More sharing options...
mikr Posted May 11, 2009 Share Posted May 11, 2009 Where you have: $error = ($_FILES["fileatt"]["error"]); for ($i=0; $i<count($error); $i++) { You may want rather, something like: $error = ($_FILES["fileatt"]["error"]); $errorcode = true; for ($i=0; $i<count($error); $i++) { if ($error[$i] == UPLOAD_ERR_OK) { $errorcode = false; } ... What may help is to look at the results of a print_r($_FILES); . You'll see that anywhere you haven't uploaded a file in the four fields, the error code will be set to 4. What you desire (with regard to error code 4), I believe, is that if any one field is UPLOAD_ERR_OK, ignore the fact that some return error code 4. That's what my code above attempts to handle. But, in addition ($_FILES["fileatt"]["error"] == 0) shouldn't work. It's an array, not a numeric variable. Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831855 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2009 Share Posted May 11, 2009 The original foreach() loop from the example in the php manual will do everything you want. The loop iterates over each of the upload file fields that exist in the form. You only need to add an else {} statement to the existing if(){} test to handle the case where the file did not upload. You should also probably use a switch/case statement with one value for each possible error number so that you don't need to use a bunch of if() tests. Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831870 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 biggest problem i have at the moment is this if ($_FILES["fileatt"]["error"] == 0) { if i upload only one valid file and no others it always returns an error because it is getting 3 errors to the value of 4 Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831880 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2009 Share Posted May 11, 2009 mikr has told you a couple of times that $_FILES["fileatt"]["error"] is an array and testing if it is == 0 does not mean anything. Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831883 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 mm still a problem with the file if i upload a valid file in fileatt 2 and no file in fileatt 1 it doesn't work $error = ($_FILES["fileatt"]["error"]); $errorcode = true; for ($i=0; $i<count($error); $i++) { if ($error[$i] == UPLOAD_ERR_OK) { $errorcode = false; } if ($errorcode ==false) { $fileatt_name = $_FILES['fileatt']['name']; $attachments1 = stripslashes($fileatt_name[0]); $attachments2 = stripslashes($fileatt_name[1]); $attachments3 = stripslashes($fileatt_name[2]); $attachments4 = stripslashes($fileatt_name[3]); } else { if ($error[$i] == 4) { $action="reply"; $sideaction=""; $error ="You did not upload any file attachment do you still wish to proceed?"; } if ($error[$i] == 2) { $action="reply"; $sideaction="addattachments"; $error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again"; } } only seems to work if a file is uploaded in the first filebox Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831908 Share on other sites More sharing options...
mikr Posted May 11, 2009 Share Posted May 11, 2009 Try having two loops. First, you check to see if any files succeeded. If they did, decide you don't care about whether any didn't. Then, second loop only if no files succeeded, then try and determine why they didn't. Only one downside, if some files succeed and others fail, you won't have any response. $error = ($_FILES["fileatt"]["error"]); $errorcode = true; for ($i=0; $i<count($error); $i++) { if ($error[$i] == UPLOAD_ERR_OK) { $errorcode = false; } } if ($errorcode ==false) { $fileatt_name = $_FILES['fileatt']['name']; $attachments1 = stripslashes($fileatt_name[0]); $attachments2 = stripslashes($fileatt_name[1]); $attachments3 = stripslashes($fileatt_name[2]); $attachments4 = stripslashes($fileatt_name[3]); } else { for ($i=0; $i<count($error); $i++) { if ($error[$i] == 4) { $action="reply"; $sideaction=""; $error ="You did not upload any file attachment do you still wish to proceed?"; } if ($error[$i] == 2) { $action="reply"; $sideaction="addattachments"; $error ="An error occured, you can only upload files with a maximum filesize of 200KB please try again"; } } Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831913 Share on other sites More sharing options...
shadiadiph Posted May 11, 2009 Author Share Posted May 11, 2009 mm I am beginning to think i might have to have a rethink about this and deal with each file one by one anyway thanks for the help i gotta get some rest just tried the above didn't make any odd like i said need to sit down and rethink it Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831928 Share on other sites More sharing options...
PFMaBiSmAd Posted May 11, 2009 Share Posted May 11, 2009 Threw this together based on the information in the php manual - <?php if(isset($_POST['submit'])){ $number_of_files = 0; // count of the number of uploaded files $error_array = array(); // array to hold the error messages foreach ($_FILES["fileatt"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["fileatt"]["tmp_name"][$key]; $name = $_FILES["fileatt"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); $number_of_files++; // increment the number of successful files } else { // there was an error for the current $key /* UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini. UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. UPLOAD_ERR_PARTIAL Value: 3; The uploaded file was only partially uploaded. UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. UPLOAD_ERR_NO_TMP_DIR Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. UPLOAD_ERR_CANT_WRITE Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0. UPLOAD_ERR_EXTENSION Value: 8; File upload stopped by extension. Introduced in PHP 5.2.0. */ switch ($error) { case 1: $error_array[] = "The size of {$_FILES["fileatt"]["name"][$key]} exceeded the upload_max_filesize setting"; break; case 2: $error_array[] = "The size of {$_FILES["fileatt"]["name"][$key]} exceeded the form's MAX_FILE_SIZE setting"; break; case 3: $error_array[] = "The file {$_FILES["fileatt"]["name"][$key]} was only partially uploaded"; break; case 4: // no file was selected, if all files are requireed, generate an appropiate user error message here break; case 6: $error_array[] = "The server is not configureed to upload files"; break; case 7: $error_array[] = "Could not save uploaded file due to a problem on the server"; break; case 8: // since there is no documentation for this error number, it probably won't be triggered for current vserions of php - $error_array[] = "The extension of {$_FILES["fileatt"]["name"][$key]} prevented the upload"; break; } } } // check for zero files uploaded (assuming at least one should have been uploaded) - if($number_of_files === 0){ $error_array[] = "No files were uploaded..."; } // output the errors - if(!empty($error_array)){ echo "The following errors occured-<br />"; foreach($error_array as $error){ echo $error . "<br />"; } } else { echo "Thank you, your form data was processed, $number_of_files file(s) were uploaded"; } } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="3000000" /> <p>files:<br /> <input type="file" name="fileatt[]" /><br /> <input type="file" name="fileatt[]" /><br /> <input type="file" name="fileatt[]" /><br /> <input type="submit" value="Send" name="submit"/> </p> </form> Link to comment https://forums.phpfreaks.com/topic/157666-solved-checking-if-files-have-uploaded-error/#findComment-831966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.