Resumable downloads when using PHP to send the file?

后端 未结 13 1171
梦毁少年i
梦毁少年i 2020-11-22 12:22

We are using a PHP scripting for tunnelling file downloads, since we don\'t want to expose the absolute path of downloadable file:

header(\"Content-Type: $ct         


        
13条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 13:18

    Yes, you can use the Range header for that. You need to give 3 more headers to the client for a full download:

    header ("Accept-Ranges: bytes");
    header ("Content-Length: " . $fileSize);
    header ("Content-Range: bytes 0-" . $fileSize - 1 . "/" . $fileSize . ";");
    

    Than for an interrupted download you need to check the Range request header by:

    $headers = getAllHeaders ();
    $range = substr ($headers['Range'], '6');
    

    And in this case don't forget to serve the content with 206 status code:

    header ("HTTP/1.1 206 Partial content");
    header ("Accept-Ranges: bytes");
    header ("Content-Length: " . $remaining_length);
    header ("Content-Range: bytes " . $start . "-" . $to . "/" . $fileSize . ";");
    

    You'll get the $start and $to variables from the request header, and use fseek() to seek to the correct position in the file.

提交回复
热议问题