使用 PHP 流式传输大文件

2022-08-30 09:18:58

我有一个200MB的文件,我想通过下载提供给用户。但是,由于我们希望用户只下载此文件一次,因此我们正在执行此操作:

echo file_get_contents('http://some.secret.location.com/secretfolder/the_file.tar.gz');

以强制下载。但是,这意味着整个文件必须加载到内存中,这通常不起作用。我们如何以每个块的kb将这个文件流式传输到他们?


答案 1

试试这样的东西(源 http://teddy.fr/2007/11/28/how-serve-big-files-through-php/):

<?php
define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk

// Read a file and display its content chunk by chunk
function readfile_chunked($filename, $retbytes = TRUE) {
    $buffer = '';
    $cnt    = 0;
    $handle = fopen($filename, 'rb');

    if ($handle === false) {
        return false;
    }

    while (!feof($handle)) {
        $buffer = fread($handle, CHUNK_SIZE);
        echo $buffer;
        ob_flush();
        flush();

        if ($retbytes) {
            $cnt += strlen($buffer);
        }
    }

    $status = fclose($handle);

    if ($retbytes && $status) {
        return $cnt; // return num. bytes delivered like readfile() does.
    }

    return $status;
}

// Here goes your code for checking that the user is logged in
// ...
// ...

if ($logged_in) {
    $filename = 'path/to/your/file';
    $mimetype = 'mime/type';
    header('Content-Type: '.$mimetype );
    readfile_chunked($filename);

} else {
    echo 'Tabatha says you haven\'t paid.';
}
?>

答案 2

使用 fpassthru()。顾名思义,它不会在发送之前将整个文件读入内存,而是将其直接输出到客户端。

根据手册中的示例进行修改:

<?php

// the file you want to send
$path = "path/to/file";

// the file name of the download, change this if needed
$public_name = basename($path);

// get the file's mime type to send the correct content type header
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $path);

// send the headers
header("Content-Disposition: attachment; filename=$public_name;");
header("Content-Type: $mime_type");
header('Content-Length: ' . filesize($path));

// stream the file
$fp = fopen($path, 'rb');
fpassthru($fp);
exit;

如果您宁愿将内容直接流式传输到浏览器而不是下载(并且如果浏览器支持内容类型,例如视频,音频,pdf等),请删除内容处置标头。


推荐