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?...
upload image under same name, and replace old image
Moderator: General Moderators
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
sure, sorry.
Here is the essential part:
"$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.
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 !!";
}
}
}
?>Just after: $clients_file_name = $userfile_name; write:
And then proceed as usual.
Code: Select all
if (file_exists($clients_file_name)) {
unlink($clients_file_name);
}-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm