Jump to content

repeating file rename


emediastudios

Recommended Posts

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

Link to comment
Share on other sites

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')") ; 

}
?>

Link to comment
Share on other sites

.. 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')") ; 

}
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 :);):D;D Fixed

Thank you very very much. ;D

Just one last thing, how would i check if file exists?

IS there a little extra code i can add?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.