I'm having some trouble with deleting a file from a website using PHP.
I have some code which uploads a file, (which works) it then resizes, renames, changes image format and saves the uploaded image twice. (Once as the full size image, once as a thumbnail.)
This part works fine, no worries.
However, I'm writing some error checking code that will delete these uploaded images if the image formats do not match the files extension.
(For example, create a bmp file in mspaint and save it. close paint, reopen the bmp file in paint then click file, save as, then save it as a png.
What happens is paint will just change the extension and not the file format. Try opening that png with my php script and it will fail with an 'image not a valid png' error.
I have written a custom error function to inform the user that the image format is bad. (Because informing users why they have an issue is better than just telling them they have an issue.)
The below code will display the name of the file, which does exist, but will not pass the 'file_exists' check.
print( $imagename . ".jpg<br/>\n" ); // Displays 'images/filename.jpg'
if ( file_exists( $imagename.".jpg" ) ) { unlink( $imagename.".jpg" ); print( "Image deleted<br/>\n" ); }
I've tried pre-pending a "/", with no luck, and I'm not really sure why the file isn't being found?
Any hints? (And apologies for the huge block of text!)
Thanks everyone for your help.
Using getcwd() confirmed that it was looking in the correct location for the file, however the issue was on my part.
My code created the new blank image, then copies the source image to it, (which is the part it would fail at) then if there was a failure, it would then copy the blank images to the final location. At the point of failure, the files do not in fact exist for file_exists to find them"
I've now checked for failure and will not copy the file if there was an issue.
Again, thanks for your help but once again, PEBCAK!