emediastudios Posted November 13, 2007 Share Posted November 13, 2007 This is the same file that i had issues with before. With help from this site i fixed the undefined varible. Now i have the problem that if not all files upload successfully the record is not inserted and i have files in my images folder that cant be overwritten(at least when i get my file exists code implemented) which makes it impossible for my client to upload the news and use these images. Can i implement a code that deletes (unlinks) the successfull uploaded images if one or two fail. That way my client can try again. Or is there a better way to get around this? Quote Link to comment Share on other sites More sharing options...
bwochinski Posted November 13, 2007 Share Posted November 13, 2007 I'd say if you can you should check if all the uploads were successful before you move anything into your "/news/" folder. Then if all are successful, move them, if not, they will be automatically discarded. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 13, 2007 Share Posted November 13, 2007 LOL meeen seems like forum helpers build this whole script any way try <?php require_once('../Connections/p2w.php'); error_reporting(E_ALL); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server $count = 0; $file = array(); for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ $photo=""; } else{ $file[] =$_FILES[$photo]['name']; //image name } } else { //Gives and error if its not ++$count; } } if (count($file) >=1){ foreach($file as $val){ unlink ("../news/".$val);// delete the file if path is correct } } if ($count > 0){ ///////////////////////line 42////////////////// echo $count .'failed to upload '; } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="5050888202"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 13, 2007 Author Share Posted November 13, 2007 The valid files still upload if one doesnt ??? Thanks for the work you did, love to get this file fixed. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 13, 2007 Share Posted November 13, 2007 <?php require_once('../Connections/p2w.php'); error_reporting(E_ALL); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server $count = 0; $file = array(); for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ $photo=""; } else{ $file[] =$_FILES[$photo]['name']; //image name } } else { //Gives and error if its not ++$count; } } if (($count > 0) || (count($file) >=1)){ ///////////////////////line 42////////////////// echo $count .'failed to upload '; if (count($file) >=1){ foreach($file as $val){ unlink ("../news/".$val);// delete the file if path is correct } } } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="5050888202"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> should work Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 13, 2007 Author Share Posted November 13, 2007 Still doing the same thing, uploads the successfull files and doesnt insert the record. Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 13, 2007 Author Share Posted November 13, 2007 I have this code in my delete news file <?php $array =array('photo1','photo2','photo3'); foreach($array as $val){ $myFile = "../news/". $_POST[$val]; if (file_exists($myFile)){ unlink($myFile); } else{?> and it works I have this one that i was helped with in this site to execute if all files dont uoload and it doesnt work. <?php if (($count > 0) || (count($file) >=1)){ echo $count .'failed to upload '; if (count($file) >=1){ foreach($file as $val){ unlink ("../news/".$val);// delete the file if path is correct } } } else {?> can i combine them or somehow intergrate the top workin code with the non working code? I dont know how.? Thanks for any help. Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 13, 2007 Author Share Posted November 13, 2007 I have been playing with this all day and i got the successfull uploads to delete if one or more files dont upload, problem is if they all upload successfully they still get deleted I know why, cause i got them all in seperate if statements. How can i take away all the if statements and have it so that if the count is wrong and the file exists then delete em. here is my poor attempt for code. <?php if (($count > 0) || (count($file) >=1)){ echo $count .' file(s) failed to upload, please make sure that all files are of valid size, format and that the file does not exsist.<br /> Please try again. '; } if (file_exists("../news/".$photo1)){ unlink ("../news/".$photo1);} if (file_exists("../news/".$photo2)){ unlink ("../news/".$photo2);} if (file_exists("../news/".$photo3)){ unlink ("../news/".$photo3); } else?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 13, 2007 Share Posted November 13, 2007 <?php require_once('../Connections/p2w.php'); error_reporting(E_ALL); //This is the directory where images will be saved $target = "../news/"; //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server $count = 0; $file = array(); for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ $photo=""; $file[] =$_FILES[$photo]['name']; //image name } else{ ++$count; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ ///////////////////////line 42////////////////// echo $count .'failed to upload '; if (count($file) >=1){ foreach($file as $val){ if (file_exists("../news/".$val)){ unlink ("../news/".$val);// delete the file if path is correct } } } } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="5050888202"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 13, 2007 Author Share Posted November 13, 2007 Thanks agin Get these errors now Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30 Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30 1failed to upload Warning: unlink(../news/) [function.unlink]: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 46 Warning: unlink(../news/) [function.unlink]: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 46 Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 13, 2007 Author Share Posted November 13, 2007 Changed the code you supplied to this below. <?php if ($count > 0){ ///////////////////////line 42////////////////// echo $count .'failed to upload '; if (count($file) >=1){ $array =array("$photo1", "$photo2", "$photo3"); foreach($array as $val){ if (file_exists("../news/".$val)){ unlink ("../news/".$val);// delete the file if path is correct } } } } else?> It works Just get this error Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30 Notice: Undefined index: in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 30 1failed to upload line 30 is this $file[] =$_FILES[$photo]['name']; //image name if i delete it the code doesnt work. I echoed some of the varibles to see what the echoed and $file was array Think that why it wasnt working before. Its already defined at the top of my code somewhere Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 13, 2007 Author Share Posted November 13, 2007 Thanks for all your help. Unbelievably happy, the errors were showing as i hade error reporting (E_ALL) the errors are minor and it works a treat. So gratefull. thanks.xx Quote Link to comment 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.