PHP Image Download - File Problems

Hi All,

New to the site. I’ve implemented a simple script to download a typical image from my server here: LINK. If I right-click and ‘Save As’, the image downloads perfectly. When I run the script to download, I can open the image file in Photoshop, but can’t view it in the browser or in Windows Photo Viewer (for example). Here is a link to a messed up downloaded image: [URL=“http://www.thenally.com/work/php-tests/header/files/herzog_dl.jpg”]LINK. I’m wondering: what is the download script doing to change the file??

PHP:

<?php

$image = "path/to/original/image.jpg";
echo $image;

header('Content-disposition: attachment; filename=herzog.jpg');
header('Content-type: image/jpeg');
readfile($image);

?>

Thanks!

Hi MNally, welcome to the forums.

Well, for one thing, header() will throw an “headers already sent” error if the script has already output anything i.e. the echo

Thanks for the reply - still no change if I remove the ‘echo’ - very strange…

The error message in Windows Photo Viewer indicates: “Windows Photo Viewer can’t open this picture because the file appears to be damaged, corrupted, or is too large.”

Again, the file size is intact, and I can open it fine in Photoshop - for some reason other programs can’t seem to recognize it as an image.

EDIT: There are no PHP errors in my log file, but this came up in my Chrome console:
"Resource interpreted as Document but transferred with MIME type image/jpeg: “http://thenally.com/work/php-tests/header/index.php”.

Was this image originally in a different format, and you changed the extension? I get this error when trying to open it:

Error interpreting JPEG image file (Not a JPEG file: starts with 0x3c 0x21)

You can try wrapping the entire script with ob_start(), just to make sure it isn’t getting corrupted due to the headers.