Noé Perracini
Noé Perracini

Reputation: 13

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.

Upvotes: 1

Views: 1528

Answers (2)

dkakoti
dkakoti

Reputation: 667

Try to set header

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

Upvotes: 1

ied3vil
ied3vil

Reputation: 939

try replacing readfile with echo file_get_contents($file)

Upvotes: 1

Related Questions