Forum Moderators: coopster

Message Too Old, No Replies

Updating an image file on the server via an update form in PHP

How do you over-write a file on the server with PHP

         

russkern

1:56 am on Nov 12, 2007 (gmt 0)

10+ Year Member



I have an image upload form that stores information about an image to a database and then gets the record ID number and re-nanes the file to match. (i.e if my record number for the image is 44 then the file is re-named on upload to the server to 44.jpg)

-- everything works well and correctly with this ---

I then wrote an Update image form, that is to update the image record in the database as any other UPDATE function does, and then using that same record id, replace the image on the server.

My understanding was (I read it somewhere but can't recall now where it was) that if you upload a file to the server and there is another file there of the same name, it would automatically overwrite the file, however, this does not seem to be the case. - the database UPDATE does work correctly however.

One note about both the add and the update script - both chmod the file to 0644 after it is uploaded.

Can anyone shed a bit of light on how I can make this happen?

fabricator

2:00 am on Nov 12, 2007 (gmt 0)

10+ Year Member



Tried deleting the old image first


$oldFile = "44.jpg";
unlink($oldFile);

russkern

2:07 am on Nov 12, 2007 (gmt 0)

10+ Year Member



OK... I'm an idiot... The script was working as it was supposed to... when I viewed the image through my page it was displaying a cached image.. when I forced a refresh a couple times I saw the correct one...

You would think I'd have thought of that first BEFORE posting...

...However...

I need to write a script to DELETE a file next... "Fabricator" ... is unlink() the correct code for this?

Also.. thanks so much for the quick reply...

R

PHP_Chimp

7:18 pm on Nov 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are just deleting the file from the database then use DELETE, if you are deleting the file from the file system on the server then you can use unlink [uk2.php.net]