upload image under same name, and replace old image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

upload image under same name, and replace old image

Post 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?...
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Could you please post the code you use to upload files.
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

Post 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.
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post 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...
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Oh, and make sure the correct permissions are set. If you have full access you can use CHMOD()
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

Post by sebnewyork »

thanx, Joe
that worked prefect
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

No problem. Glad it helped.
Post Reply