Hi I want users to be able to upload new images that overwrite old images but I am struggling. The original upload and naming works fine but I want any new images to have the same name, so I unlink the original before uploading the new image, is this the correct way to do it?

If it is then I am doing something wrong as it does not put the new image in. I think it may be something to do with unlinking, I don't really get how to do it, the file $new_file_name is in /upload/ and here is the code I have:

if(file_exists("upload/' . $new_file_name . '")) unlink("upload/' . $new_file_name . '");

I don't know how to put $new_file_name in there, could someone help?

Thanks

Recommended Answers

All 6 Replies

if (file_exists("upload/$new_file_name")) 
    unlink("upload/$new_file_name");

Thanks pritaeas. However I was wrong that wasn't the problem... it still isn't working, CHMOD is set to 777 so I do not know why, I would be very grateful if you could have a look:

$link = mysql_connect(''''''); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} else { echo 'connected'; }
mysql_select_db(); 

$pagename = $_POST['pagename'];
$pagename = mysql_real_escape_string($pagename);

echo $pagename;

$query = ("SELECT * FROM bands WHERE PageName='$pagename'");

$result = mysql_query($query);

$row = mysql_fetch_array($result);

$bandid = $row['id'];

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["ufile"]["name"]));
if ((($_FILES["ufile"]["type"] == "image/gif")
|| ($_FILES["ufile"]["type"] == "image/jpeg")
|| ($_FILES["ufile"]["type"] == "image/pjpeg"))
&& ($_FILES["ufile"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["ufile"]["error"] . "<br />";
    }
else {
$file_name = $HTTP_POST_FILES['ufile']['name'];

$random_digit= $bandid ;

$new_file_name=$random_digit.'.'.$extension;

if (file_exists("upload/$new_file_name")) 
    unlink("upload/$new_file_name");

$path= "upload/".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>"; 

echo "File Name :".$new_file_name."<BR/>"; 

echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>"; 

echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>"; 

echo $bandid;

$buery = ("UPDATE bands SET imageurl = '$path' WHERE id='$bandid'");

$besult = mysql_query($buery); { echo 'inserted';}
}
else
{
echo "Error";
}
}
}
  }

Thanks in advance

it still isn't working

What's not working?

Instead of copy you should try move_uploaded_file

Hi, I changed it to move_uploaded_file but still having problems. It doesn't echo any of the messages after move_uploaded_file, and the file isn't uploaded, and as it works if there is no file uploaded already with that name I think the issue is with this part:

if (file_exists("upload/$new_file_name")) 
    unlink("upload/$new_file_name");
$path= "upload/".$new_file_name;
if($ufile !=none)
{
if(move_uploaded_file($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{

But I have never done this so don't really know what the problem is.

THanks

Check your error log.

Hi there is nothing in the error log, are there any error reporting lines I can put in to help?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.