Page 1 of 1
upload image under same name, and replace old image
Posted: Fri Dec 17, 2004 12:25 pm
by sebnewyork
Hi all
I am building a simple PHP content management interface. I figured out how to let my clients upload images to the server.
Now, If I want to replace an image that's currently on the server with a new one, I thought I'd give the same name to the new one, and I expected it to automatically replace the old one.
However I noticed that when I try to do just that, the upload fails. Apparently it doesn't work that way, and I can't upload an image with the same name to delete/replace an old one.
But that's what I want to do.
Any solution?...
Posted: Fri Dec 17, 2004 12:34 pm
by Joe
Could you please post the code you use to upload files.
Posted: Fri Dec 17, 2004 12:41 pm
by sebnewyork
sure, sorry.
Here is the essential part:
Code: Select all
<?php
if ( $submitted ) {
if ( $userfile == "" ) {
$error_msg ="<p>Your Upload was not accepted.<br> Verify that file size is < $MAX_FILE_SIZE bytes and that the file you specified exists. </p>";
}else{
$clients_file_name = $userfile_name;
$temp_file_name = basename ($userfile );
$arr_basename = explode (".", $clients_file_name );
$file_type = $arr_basename [1];
if (( $file_type == "jpg" ) || ( $file_type == "gif" )) {
$new_file_name = $username.".".$file_type;
$temp_full_path = $userfile;
$final_full_path = $final_location.$new_file_name;
if (! copy ($temp_full_path, $final_full_path )) {
$error_msg = "failed to copy" . $file . "...";
}
}else{
$error_msg = "Upload Failed - jpg or gif types only please !!";
}
}
}
?>
"$username" is the name entered in a text filed that I choose to give the image once uploaded. I expected that if "$usrename" + file extension matched an existing file already on the same folder, it would replace it. But instead, it won't upload it.
Posted: Fri Dec 17, 2004 12:48 pm
by lostboy
how does it fail?
what is the error?
how is the image stored?
what code are you using?
its friday and my mindreading skills are done in for th week...
Posted: Fri Dec 17, 2004 12:58 pm
by Joe
Just after: $clients_file_name = $userfile_name; write:
Code: Select all
if (file_exists($clients_file_name)) {
unlink($clients_file_name);
}
And then proceed as usual.
Posted: Fri Dec 17, 2004 12:58 pm
by Joe
Oh, and make sure the correct permissions are set. If you have full access you can use
CHMOD()
Posted: Fri Dec 17, 2004 1:23 pm
by sebnewyork
thanx, Joe
that worked prefect
Posted: Fri Dec 17, 2004 1:27 pm
by Joe
No problem. Glad it helped.