emediastudios Posted November 6, 2007 Share Posted November 6, 2007 I have this error in my code that when i upload 3 images it adds the previous file name to the end of the next files name, very wierd. here is the code <?php require_once('../Connections/p2w.php'); //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 for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } ?> this is the resulting file names in my news folder after uploading 3 files originally named 11.jpg,12.jpg,19.jpg 11.jpg, 11.jpg12.jpg, 11.jpg12.jpg.19.jpg Quote Link to comment Share on other sites More sharing options...
d.shankar Posted November 6, 2007 Share Posted November 6, 2007 Try Now <?php require_once('../Connections/p2w.php'); //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 for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Does the same thing ??? what did u change? Quote Link to comment Share on other sites More sharing options...
bwochinski Posted November 6, 2007 Share Posted November 6, 2007 This line is your culprit: <?php $target = $target . basename( $_FILES[$photo]['name']); ?> You need to reinitialize the $target variable each iteration of the loop. Quote Link to comment Share on other sites More sharing options...
d.shankar Posted November 6, 2007 Share Posted November 6, 2007 .. Try again if it clicks <?php require_once('../Connections/p2w.php'); //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 for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; //Writes the information to the database } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 This line is your culprit: <?php $target = $target . basename( $_FILES[$photo]['name']); ?> You need to reinitialize the $target variable each iteration of the loop. Sorry to be unable to do what you recommend as im still learning php. Had an idea that that was cuasing problems but how i do as you say? Thanks for your help Quote Link to comment Share on other sites More sharing options...
d.shankar Posted November 6, 2007 Share Posted November 6, 2007 Did you try the code i posted above ? I remodified it. Quote Link to comment Share on other sites More sharing options...
bwochinski Posted November 6, 2007 Share Posted November 6, 2007 Well, the easiest way to fix the problem at this point would be just to change that line to this: <?php $target = "../news/" . basename( $_FILES[$photo]['name']); ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Did you try the code i posted above ? I remodified it. did the same thing champ Quote Link to comment Share on other sites More sharing options...
d.shankar Posted November 6, 2007 Share Posted November 6, 2007 Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Well, the easiest way to fix the problem at this point would be just to change that line to this: <?php $target = "../news/" . basename( $_FILES[$photo]['name']); ?> YOUR DA MAN Fixed Thank you very very much. Just one last thing, how would i check if file exists? IS there a little extra code i can add? Quote Link to comment Share on other sites More sharing options...
d.shankar Posted November 6, 2007 Share Posted November 6, 2007 <?php $filename = "../news/11.jpg"; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> 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.