I have put together a basic PHP script, that, when executed, should trigger a download of file stored on server. I took note from http://php.net/manual/en/function.readfile.php for using readfile function, and used identical code.
This used to work earlier, but now, suddenly, I've started getting 404 pages instead. Below is the snippet that I have :
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($serverPath));
readfile($serverPath);
exit(0);
When I insert debugging scripts in between, I'm able to see them, along with the weird data that readfile
function receives from file, as files are PDF. But, on their own, this just gives me a 404 page with no error/warning anywhere.
Can someone please point me to any issue that I'm having in code, or any thing that I can look into ? Any pointers would be very helpful, as I've spent almost all the day through forums and debugging, and still nowhere near to any kind of solution.
Edit :
Below are the response headers received from server :
Date: Tue, 21 Feb 2017 23:01:50 GMT
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/1.0.1e-fips mod_bwlimited/1.4
X-Powered-By: PHP/5.4.45
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Content-Disposition: attachment; filename="QM-1.pdf"
Keep-Alive: timeout=2, max=150
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/octet-stream
Edit 2 :
So, I tried the localhost approach, and the code indeed works perfectly fine in my local environment. PHP versions are same at both locations. Apart from that, any pointers where I should look at server level to find the cause behind this behavior ?