Search code examples
phpjpeg

PHP cant download image with readfile


This is my code:

$file = 'test.jpg';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);

test.jpg is a file on my system. I can open it and nothing is wrong with it. The download gets started but when i try to open the downloaded image, it says that it cant be opened. Im testing it on Chrome. Does someone have a solution? Thanks a lot.

Edit: the downloaded .jpg is only 23kb where as the file i want is 135kb.


Solution

  • Try to set header

    header('Content-Type: image/jpeg');
    ob_clean();
    exit;