dpn68721 2015-09-02 21:16
浏览 29

PHP下载脚本失败

Good Day Everyone,

I need to use a PHP script to force Download a some MP3 (I want to make sure that it doesn't start streaming when clicking on the MP3 link). Here is the script am using right now:

<?php 

@ini_set('error_reporting', E_ALL & ~ E_NOTICE);
@ini_set('zlib.output_compression', 'Off');

if(!isset($_GET['id']) || empty($_GET['id'])) 
{
    header("HTTP/1.0 400 Bad Request");
    echo ("No ID Give");
    exit;
}

$filelist = array(
    '000001' => array(
        'filename' => 'djmix.mp3',
        'folder' => 'http://www.example.com/music/download/',
        'serverfldr' => '/home/content/html/music/download/'),
    '000002' => array(
        'filename' => 'othermix.mp3',
        'folder' => 'http://www.example.com/music/download/',
        'serverfldr' => '/home/content/html/music/download/')
    );

$id = $_GET['id'];
$file  = $filelist[$id]['filename'];
$folder = $filelist[$id]['folder'];
$serverpath  = $filelist[$id]['serverfldr'];

if (is_file($serverpath . $file)){

    $filesize = filesize($serverpath . $file);
    $file = @fopen($serverpath . $file, "rb");
    if ($file){
        header('Pragma: public');
        header('Expires: -1');
        header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
        header('Content-Disposition: attachment; filename="' . $file . '"');
        header('Content-Type: application/octet-stream');
        header('Content-Length:' . $filesize);

        set_time_limit(0);

        while(!feof($file)) 
        {
            print(@fread($file, 1024*8));
            ob_flush();
            flush();
            if (connection_status()!=0) 
        {
            @fclose($file);
            exit;
        }           
    }
        // file save was a success
        @fclose($file);
        exit;

    }
    else{
        echo ('File cannot be opened');
    }

}
else {
    echo('Cannot find a file');
};

exit;
?>

My biggest problem is that the files are quite big (from 50MB to 100MB), but it seems that on slow connection, there is some kind of timeout... Or Something happening because the download actually fails. (It really writes "FAILED" in Firefox).

On Firefox on my slow internet it gives me "FAILS" If I use a download manager (Speed Download for Mac) the download actually RESETS... Yes yes you read properly, it resets... What I mean by that? The download goes for a while and after a certain time it restarts from Zero.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 slam rangenet++配置
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊