0

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 ?

Prateek
  • 274
  • 1
  • 4
  • 18
  • try to change your Content-Type to application/pdf. Maybe the problem is here. Also I think this links are similar to your situation: http://stackoverflow.com/a/10088979/6066986 http://stackoverflow.com/a/13570935/6066986 – Grynets Feb 21 '17 at 22:53
  • @Grynets Tried pdf option and also tried file options, no success with either of them. – Prateek Feb 21 '17 at 22:54
  • Has anything changed on your server? Maybe some permissions? – Grynets Feb 21 '17 at 22:58
  • That's what I'm thinking as of now, but no idea what to check. And since server is outside of my control, I don't have access to much data. Any areas I should specifically look into ? I checked with permissions and they are all set correctly. – Prateek Feb 21 '17 at 23:00
  • Try to recreate situation on your local machine. Put your file in dir, than create index.php file with your code in question for downloading, and than stand up your local server with `php -S localhost`. In your browser go there and see what's the problem. I think this is the easiest way to check everything. – Grynets Feb 21 '17 at 23:05

1 Answers1

0

Have you checked if the file exists and is readable? Consider using functions like file_exists and is_readable to ensure your code works as planned.

Be sure to also check the headers you get when the server returns a HTTP 404 error, you might not even reach your code, and the error may be elsewhere.

3472948
  • 801
  • 4
  • 15
  • Already tried with those functions and no success. Also, I manually checked, and files are present. If I visit that file via direct URL, it works normally. I've added headers received from server in question. – Prateek Feb 21 '17 at 23:05
  • you can use the `is_readable($file)` function, to check if the file exists before reading it. – Khalil Jan 15 '23 at 17:13