开发者

What is the difference between a normal file download and a PHP-driven download?

开发者 https://www.devze.com 2023-03-10 02:13 出处:网络
Is there a difference between directly downloading a file from a web server, and downloading the same file through a PHP script that uses these headers?(Assume that all of the variables here contain c

Is there a difference between directly downloading a file from a web server, and downloading the same file through a PHP script that uses these headers? (Assume that all of the variables here contain correct values for the file being down开发者_如何学运维loaded.)

header('Content-Description: File Transfer');
header('Content-Type: ' . $mimeType);
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

After reading the discussion here, i want to change the question :

With a file on server for user who can download.

What & when do we need to process this task by PHP script?

Same question with direct link of file


Not as far as the client is concerned, no.

The difference for the server is that you have the full power of PHP to do something with before initiating the download, which is not the case if Apache handles the download directly. It also means that the whole cruft of PHP needs to be loaded and executed before a download can start and it'll occupy one PHP process until the download is finished.


If your "normal" download would supply those exact headers, then no.

However, a number of things could cause the server to decide to send different headers:

  • If gzip is turned on (and browser accepts it)
  • If the request sent an If-Modified-Since header and the content has not been modified
  • If the server decides to send a Last-Modified time
  • If the server has some other sort of cache-control logic


I would think this would also let you create a download for a resource that is normally forbidden for a user to directly access. It also seems like you could use this for a file that only exists in memory, without having to write it out to disk first.

0

精彩评论

暂无评论...
验证码 换一张
取 消