Replacing images with PHP
I was thinking if i need to replace an image on the server with an uploaded version, does it follow the same fopen+fwrite functions calls like writing/appending a string to a txt file?
My other option is to delete the existing file and replace it with the uploaded version with the same name but i want to know if one cant simply replace it rather than deleting+replacing.
I cant seem to find any clues on the internet.
if you open the files using fopen be sure to do so using binary mode.
$fo = fopen( 'image.jpg', 'br' );
However, for binary data like an image it is best to just overwrite the old image with the new one.
Logic without the fatal effects.
All code snippets are licensed under WTFPL.
writing to the jpg file doesnt work as expected. The file size of the replaced image drops to 13bytes and image cannot be previewed.
I have switched to delete the existing image and replacing with a new one - just seems simpler and less hassling.
Just read in the documentation that move_uploaded_file function overwrites the file with the same name if it does exist.