readfile()

まず、file_get_contents()

1
2
3
4
5
6
$image = file_get_contents(__DIR__ . '/sample.jpg');
header('Content-Type: image/jpg');
header('Content-Length: '. count($image));
header('Content-Disposition: attachment; filename=sample.jpg');
 
echo $image;

続いて、readfile()

1
2
3
4
5
6
$filename = (__DIR__ . '/sample.jpg');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($filename));
header('Content-Disposition: attachment; filename=sample.jpg');
 
readfile($filename);

いいですねー
まあ、フレームワークによって作法があるようです。