Serving large files with PHP

前端 未结 9 988
北海茫月
北海茫月 2020-12-13 07:51

So I am trying to serve large files via a PHP script, they are not in a web accessible directory, so this is the best way I can figure to provide access to them.

The

9条回答
  •  情深已故
    2020-12-13 08:16

    The best way to send big files with php is the X-Sendfile header. It allows the webserver to serve files much faster through zero-copy mechanisms like sendfile(2). It is supported by lighttpd and apache with a plugin.

    Example:

    $file = "/absolute/path/to/file"; // can be protected by .htaccess
    header('X-Sendfile: '.$file);
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    // other headers ...
    exit;
    

    The server reads the X-Sendfile header and sends out the file.

提交回复
热议问题