PHP Download Page

前端 未结 5 741
我在风中等你
我在风中等你 2021-01-29 07:13

How do these PHP download pages (e.g. somesite.com/download.php?id=somefile) often found work?

I originally thought of a page that does the counter stuff, and then simpl

5条回答
  •  失恋的感觉
    2021-01-29 08:01

    Most often it is done by using php function readfile:

    if(is_allowed_to_download()){
        header("Content-Disposition: attachment; filename=".basename( $file ));
        header("Content-Type: application/octet-stream");
        header("Content-Length: ". filesize( $file ) );
        header("Content-Transfer-Encoding: binary");
    
        @readfile($file);
        exit;
    }
    

    Less often server specific solutions is used - sending header X-SendFile: file-location.exe for lighttpd and apache with mod_xsendfile (nginx also have some equivalent). These are slight better, because http servers are optimized to serve content and allow for advanced usage like Range headers (for download accelerators).

提交回复
热议问题