php 实现大文件切片下载

版权声明:本文为博主原创文章,转载请附上出处。 https://blog.csdn.net/k_runtu/article/details/82180868
function down_file1($filename, array $allowDownExt=['jpg', 'jpeg', 'gif', 'zip', 'php', 'txt']) {
    if (!is_file($filename) || !is_readable($filename)) {
        return false;
    }

    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

    if (!in_array($ext, $allowDownExt)) {
        return false;
    }

    header('content-type:application/octet-stream');
    header('Accept-Ranges:bytes');
    header('Accept-Length:'.filesize($filename));
    header('Content-Disposition:attachment;filename='.basename($filename));

    $filesize = filesize($filename);

    $read_buffer = 1024;
    $sum_buffer = 0;
    $handle = fopen($filename, 'rb');
    while(!feof($handle) && $sum_buffer<$filesize) {
        echo fread($handle, $read_buffer);
        $sum_buffer += $read_buffer;
    }
    fclose($handle);

    exit;
}

猜你喜欢

转载自blog.csdn.net/k_runtu/article/details/82180868
今日推荐