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
https://forums.phpfreaks.com/topic/76172-repeating-file-rename/
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
https://forums.phpfreaks.com/topic/76172-repeating-file-rename/#findComment-385512
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
https://forums.phpfreaks.com/topic/76172-repeating-file-rename/#findComment-385523
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
https://forums.phpfreaks.com/topic/76172-repeating-file-rename/#findComment-385540
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
https://forums.phpfreaks.com/topic/76172-repeating-file-rename/#findComment-385552
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.