My bad. I'm such a smurf!
This code works:
// check if the client validating cache and if it is current
$ifModified = Request::header('If-Modified-Since');
if (isset($ifModified) && (strtotime($ifModified) == filemtime($filePath . $fileName))) {
// cache IS current, respond 304
header('HTTP/1.0 304 Not Modified');
exit();
} else {
return new BinaryFileResponse($filePath . $fileName, 200,
array(
'Content-Type' => 'image/jpeg', // Guessing probably all jpegs.
'Content-Transfer-Encoding' => 'binary',
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
'Content-Length' => filesize($filePath . $fileName),
'Expires' => date(DATE_RFC822, strtotime("+2 days")),
'Last-Modified' => date(DATE_RFC822, \Illuminate\Support\Facades\File::lastModified($filePath . $fileName)),
'Cache-Control' => 'public, max-age=10800, pre-check=10800',
'Pragma' => 'public',
), true, 'inline');
}
Since I'm returning a Binary File Response, I of course should not read the file before handing it over. The response constructor expects a file name, not the data. Anyway, this uncovered that there were never any problems with the response headers at all.
The response failed and I got the headers accordingly. But since I read in the data from the image file, the data went along as the body of the request anyway. Which was the reason I did not catch the error.