Image download with PHP header: Images not displaying in Windows Photo Viewer and MacOS Preview

607 views Asked by At

I have a problem using image download with the PHP header function: The image is downloading properly, can be viewed in Photoshop, but cannot be opened in Windows Photo Viewer and MacOS Preview. Here ist the (simple) code I am using:

header('Content-type: image/jpg');
header('Content-Disposition: attachment; filename="'.$url.'"');
readfile($image);

Thank you so much,

Alex

1

There are 1 answers

0
AlexTheNoob On

I found the solution (maybe interesting for others): Simply do not execute any HTML/JavaScript code, before the PHP Header code is executed. Otherwise the HTML/Javascript code is written inside the image file and therefore it cannot be displayed in Windows Photo Viewer etc. Photoshop seems to filter it out somehow.