View Full Version : Overwriting image

Jul 9th, 2002, 09:32 PM
I'm back with another uploader question. How can I overwrite the image that is already in the users directory? The user can have either gif or jpg image, but are only allowed one image. So if they first uploaded a jpg, then a gif later on, it shows both the images. How can I get the different file formats to overwrite eachother?

If the code is in the uploader, here's the upoloader script

echo "";

Jul 9th, 2002, 10:33 PM
The best way would be to delete the old image (if it exists). At least thats the only way I can think of but I don't know much about directory and file functions.

Jul 10th, 2002, 12:52 AM
I don't know anything about them either. I've been thinking that it may be best to rename the image to the extension that's in the direcetory, but I have no idea how to do that either. The uploader has thrown me off coz I have no idea how it works.

Jul 10th, 2002, 01:24 AM
Just scan the directory for any file ending .gif, .jpeg or .jpg. If
there is one - delete it.

Then just pop the uploaded image in there.

Jul 10th, 2002, 04:08 AM
Would you mind showing me how to do that? If it's a lot of work, then you don't have to show me. I'll try and figure out a new way of doing it.

Jul 10th, 2002, 05:44 AM
Do you rename their files once they upload them? Also you might want to make sure the new image successfully uploads before deleting the old one .

Jul 10th, 2002, 05:49 AM
It's renamed to their username and then put into the users directory. I think that's how it happens, FirePages was the one that gave me the uploader but I can't figure it out myself.

I came across a problem earlier though :( I can't overwrite anything in the directory, nor delete, nor rename. Once the file's there, it's there forever, or until my host deletes it :(

I guess I gotta find out how to upload to mysql.

Jul 10th, 2002, 07:30 AM
if your script created the image as indeed the uploader does you should be able to unlink it??



and you need to do that before your upload script starts else you may overwrite the newly uploaded image :)

EDIT : added the clearstatcache() as alledgedly the call to file_exists() is cached by PHP and could affect other calls to file_exists()

Jul 10th, 2002, 06:31 PM
Thanks :)

Thats weird how thie file thing is working. It's overwriting and deleting the images now, but still won't delete a directory. :confused:

Jul 10th, 2002, 08:03 PM
is the directory empty ? PHP cant delete a directory with contents, you can do it via exec() but that did not seem to work when you tried it?

Jul 10th, 2002, 08:40 PM
Just a lil odd note:

If you upload a .gif with PHP and then name it ***.jpg it becomes
a jpeg :) - transparent colours become white. That's with using