PHP提供了readfile函数可以用作文件下载,这个函数对于10M以内的小文件也完全足够。 但是当下载文件达到几百M的时候,就可能会出现报错:Allowed memory size of bytes exhausted。

这时的解决方案是使用 fopen函数自己读取文件,并在输出的时候,使用 ob_flushflush函数, 定时清除缓冲区内容,解决内存问题。

代码实现

使用 fopen函数读取文件并输出内容,使用ob_flushflush函数定时刷新输出缓存区。

<?php
function file_down($filepath, $file_name = ''){
    set_time_limit(0);

    if (!$file_name){
        $file_name = basename($filepath);
    }

    $filesize = filesize($filepath);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Accept-Ranges: bytes');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . $filesize);
    header('Content-Disposition: attachment; filename=' . $file_name);

    $index = 0;
    $read_buffer = 4096;
    $fp = fopen($filepath, 'rb');
    //  echo '开始内存:'.memory_get_usage(), PHP_EOL;
    while(!feof($fp)) {
        echo fread($fp, $read_buffer);
        $index++;

        if ($index % 1024 == 0){
            //  echo '运行中内存:'.memory_get_usage(), PHP_EOL;
            ob_flush(); // 冲刷出输出缓冲区中的内容
            flush();    // 刷新输出缓冲到浏览器
        }
    }
    //  echo '运行后内存:'.memory_get_usage(), PHP_EOL;
    // Close the file
    fclose($fp);
}