emediastudios Posted October 20, 2007 Share Posted October 20, 2007 I have this code im still working on. I have this problem that if some of the files were not valid for upload, it still uploads the ones that were, which is good in a way, only problem is that it doesnt insert a record and when they retry, they get the error " image exists" which i understand. and there is no way of deleting the files or overrighting them. except manually doing it. Down the line i will set up an option to update the record and allow them to re upload the failed images, but i have posted the website and my client needs a quick fix for now. What i am after is if some of the posted files upload successfully, create the record. Code <?php //This is the directory where images will be saved $path = '../images/'; //This gets all the other information from the form $name=$_POST['name']; $suburb=$_POST['suburb']; $price=$_POST['price']; $content=$_POST['content']; $content2=$_POST['content2']; $agentmobile=$_POST['agentmobile']; $agentemail=$_POST['agentemail']; $uploadFile0=($_FILES['uploadFile0']['name']); $uploadFile1=($_FILES['uploadFile1']['name']); $uploadFile2=($_FILES['uploadFile2']['name']); $uploadFile3=($_FILES['uploadFile3']['name']); $uploadFile4=($_FILES['uploadFile4']['name']); $uploadFile5=($_FILES['uploadFile5']['name']); $uploadFile6=($_FILES['uploadFile6']['name']); $uploadFile7=($_FILES['uploadFile7']['name']); $uploadFile8=($_FILES['uploadFile8']['name']); // Connects to your Database mysql_connect("localhost", "root", "*********") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; // Uploads Images $uploadNeed = $_POST['uploadNeed']; // start for loop $copied = 0;//the number of files successfully uploaded for($x=0;$x<$uploadNeed;$x++) { $file_name = $_FILES['uploadFile'. $x]['name']; //test $Size = $_FILES['uploadFile'. $x]['size']; //Test Check $Valid = false; if(valid_ext($file_name)) { echo " {$file_name} valid ext"; $Valid = true; }else{ echo " {$file_name} invalid Type"; } echo "</br>"; if(valid_size($Size)) { echo " {$file_name} valid size</br>"; }else{ $Valid = false; echo " {$file_name} invalid Size</br>"; } if($Valid) { // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($path . $file_name) ) { echo "<br>The file {$file_name} already exists.<br>"; }else { $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); $copied++;//increment our counter } } } // check if successfully copied if($copied == $uploadNeed) { print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; }else{ echo "<br>$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, jpeg, jpg, png or gif!<br /> <br /> Please go <a href=\"property_add.php\">back</a> and try again"; } // end of loop //***FUNCTIONS //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } //filter by size, function valid_size($size) { return ($size <= 1048576); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/ Share on other sites More sharing options...
emediastudios Posted October 20, 2007 Author Share Posted October 20, 2007 My topic doesnt match my Question Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-373865 Share on other sites More sharing options...
steve448 Posted October 20, 2007 Share Posted October 20, 2007 Why not start by assuming that all the $uploadFile variables = '' Then if it passes all your validation checks set it to the filename. You can then finish with: <?php if($copied > 0) { //do your insert stuff if($copied != $uploadNeed) { $not_uploaded = $uploadNeed - $copied; echo $not_uploaded . ' files were not uploaded, please make sure they are valid file types etc.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-373890 Share on other sites More sharing options...
emediastudios Posted October 21, 2007 Author Share Posted October 21, 2007 Thanks, thats a good add to my code. It doesnt add the record unless all files are uploaded, still im left with the problem that the successfull uploaded files are in my images directory and i cant re insert the record as it echos "image already exsists" What i want is to delete the successfull uploaded files and they can go back and try again. If i could insert a record and then redirect them to an update record page and let them rectify the errors for that record, that would be awesome, i will work on that in the future, but 4 now i need a quick fix. It is not a feature for all website uses than rather the admin to add properties to his/her real estate site. I have this code i use on my delete property page. $myFile = array(); for($i=1;$i<=9;$i++) { $myFile[$i] = "../images/" . $_POST['photo' . $i]; if (!empty($_POST['photo'.$i])) unlink($myFile[$i]); } Can i implement that in mine so that if one fail do this code and unlink Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-374671 Share on other sites More sharing options...
steve448 Posted October 21, 2007 Share Posted October 21, 2007 You don't need to. When you are going through your validation checks in your script, if it fails at any point just get rid of the file then. I.e. <?php if(!$Valid) { unlink($file_name); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-374679 Share on other sites More sharing options...
emediastudios Posted October 21, 2007 Author Share Posted October 21, 2007 On the right track. I have this code and im sure my answer is in the following area of my code. I used a snippit of your and put it here like this <?phpif($copied > 0) { //do your insert stuff if($copied != $uploadNeed) { $not_uploaded = $uploadNeed - $copied; echo $not_uploaded . ' files were not uploaded, please make sure they are valid file types etc.'; unlink($path . $uploadFile0); } }?> The $uploadFile0 needs to be changed to include all successfull file uploads only. How wold i do that? , im still learning php but feel im getting a drift of things, Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-374710 Share on other sites More sharing options...
emediastudios Posted October 22, 2007 Author Share Posted October 22, 2007 Is there a way i could do something like this. $uploadedFiles = (successfully uploaded files only) <<<<------- need the right code here so that in my code i could have <?php if($copied != $uploadNeed) { $not_uploaded = $uploadNeed - $copied; echo $not_uploaded . ' files were not uploaded, please make sure they are valid file types etc.'; unlink($path . $uploadedFiles); ?> Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-375308 Share on other sites More sharing options...
yzerman Posted October 22, 2007 Share Posted October 22, 2007 My question: Is it out of the question to do file checking - and if any errors exist, stop the code, and let them know file#2 is a bad file, and they need to replace or remove it before they can upload the rest of the files? Think of it this way: Your registering for a forum - you input an invalid password, but the rest of the information is correct. Do you create the database entry before you make him fix the password? If not why would you do it any other way for a file system? Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-375313 Share on other sites More sharing options...
emediastudios Posted October 22, 2007 Author Share Posted October 22, 2007 no, the record is not inserted, if there is errors in some of the uploaded files but some of the files may have been successfully uploaded and they will exist in the directory, they can not overright these images, as my code wil return the error, file already exsits, therefor they wont be able to insert a record. Does that make sense? I just want to delete all successfull uploaded files if there were errors and they can try again after they rectify the files that werent compatible for upload? Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-375316 Share on other sites More sharing options...
emediastudios Posted October 22, 2007 Author Share Posted October 22, 2007 In a nutshell - What i need is a code that checks for what files were uploaded successfully, and at the end of my code when it does all the validation checks, if errors where reported, delete them all. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-375318 Share on other sites More sharing options...
emediastudios Posted October 22, 2007 Author Share Posted October 22, 2007 The code i need is something like $successfullyUploaded = (dont know what to put here) so then i can put <?php if($copied > 0) { //do your insert stuff if($copied != $uploadNeed) { $not_uploaded = $uploadNeed - $copied; echo $not_uploaded . ' files were not uploaded, please make sure they are valid file types etc.'; unlink($path . $successfullyUploaded); } ?> This is all i need to finish off this 3 week nightmare Thanks for any help given My full code below <?php //This is the directory where images will be saved $path = '../images/'; //This gets all the other information from the form $name=$_POST['name']; $suburb=$_POST['suburb']; $price=$_POST['price']; $content=$_POST['content']; $content2=$_POST['content2']; $agentmobile=$_POST['agentmobile']; $agentemail=$_POST['agentemail']; $uploadFile0=($_FILES['uploadFile0']['name']); $uploadFile1=($_FILES['uploadFile1']['name']); $uploadFile2=($_FILES['uploadFile2']['name']); $uploadFile3=($_FILES['uploadFile3']['name']); $uploadFile4=($_FILES['uploadFile4']['name']); $uploadFile5=($_FILES['uploadFile5']['name']); $uploadFile6=($_FILES['uploadFile6']['name']); $uploadFile7=($_FILES['uploadFile7']['name']); $uploadFile8=($_FILES['uploadFile8']['name']); // Connects to your Database mysql_connect("localhost", "root", "********") or die(mysql_error()) ; mysql_select_db("gcproperty") or die(mysql_error()) ; // Uploads Images $uploadNeed = $_POST['uploadNeed']; // start for loop $copied = 0;//the number of files successfully uploaded for($x=0;$x<$uploadNeed;$x++) { $file_name = $_FILES['uploadFile'. $x]['name']; //test $Size = $_FILES['uploadFile'. $x]['size']; //Test Check $Valid = false; if(valid_ext($file_name)) { echo " {$file_name} valid ext"; $Valid = true; }else{ echo " {$file_name} invalid Type"; } echo "</br>"; if(valid_size($Size)) { echo " {$file_name} valid size</br>"; }else{ $Valid = false; echo " {$file_name} invalid Size</br>"; } if($Valid) { // strip file_name of slashes $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($path . $file_name) ) { echo "<br>The file {$file_name} already exists.<br>"; }else { $copy = move_uploaded_file($_FILES['uploadFile'. $x]['tmp_name'], $path . $file_name); $copied++;//increment our counter } } } if($copied > 0) { //do your insert stuff if($copied != $uploadNeed) { $not_uploaded = $uploadNeed - $copied; echo $not_uploaded . ' files were not uploaded, please make sure they are valid file types etc.'; unlink($path . $file_name); } } // check if successfully copied if($copied == $uploadNeed) { print "<meta http-equiv=\"refresh\" content=\"0;URL=property_added_successfully.php\">"; //Writes the information to the database mysql_query("INSERT INTO `employees` VALUES ('$name', '$suburb', '$price', '$content', '$content2','$agentmobile', '$agentemail','$uploadFile0','$uploadFile1', '$uploadFile2', '$uploadFile3', '$uploadFile4', '$uploadFile5', '$uploadFile6', '$uploadFile7', '$uploadFile8')") ; }else{ echo "<br>$file_name The File(s) could not be uploaded!<br>The file must be under 1 meg and be of a valid extension type, jpeg, jpg, png or gif!<br /> <br /> Please go <a href=\"property_add.php\">back</a> and try again"; } // end of loop //***FUNCTIONS //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } //filter by size, function valid_size($size) { return ($size <= 1048576); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74053-not-upload-images-if-one-or-more-fail/#findComment-375335 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.