PHP - CodeIgniter下载远程文件

时间:2012-08-07 16:16:33

标签: php codeigniter

我在下载文件时遇到CodeIgniter问题..

代码在单独的php文件中完美运行,但是当我将代码放在CodeIgniter中时: 该文件将成功下载,但它是损坏:(。

注意:我正在处理来自远程服务器的视频文件。

代码:

$file = fopen ($link, "r");
if (!$file) {
    echo "<p align='center' style='color:#FF0101;'>Unable to open remote file :(, Please try again in a few minutes.</p>";
}else{
    ob_clean();
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    header('Content-Disposition: attachment; filename=zxc.3gp');
    //header("Content-Transfer-Encoding: binary");
    //header("Content-Length: ".$video_size);


    while (!feof ($file)) {
        $line = fgets ($file, 1024);
        echo $line;
        ob_flush();
        flush();
    }
    fclose($file);
    die();
}

我在新的CodeIgniter项目中尝试了这段代码:

public function index()
{
    $link = 'http://mamprogr.net.tc/tmp/1.3gp';
    $this->load->helper('download');
    force_download('1.3gp',$link);
}

但不工作:(

3 个答案:

答案 0 :(得分:4)

尝试CodeIgniter的force_download()帮助,看看是否有帮助

force_download($name, $data); 

<强>更新

我注意到你试图提供一个直接链接作为force_download()函数的第二个参数 - 但是,它需要“数据” - 见下文 -

$data = file_get_contents("/local/path/to//1.3gp"); // Read the file's contents
//or perhpas $data = fopen(......);
$name = '1.3gp';

force_download($name, $data); 

答案 1 :(得分:0)

您的解决方案解决了下载问题。但是当文件播放时你会得到一个错误:

Windows Media Player无法播放该文件。播放器可能不支持该文件类型,或者可能不支持用于压缩文件的编解码器。

我尝试了quicktime和realplayer,但视频仍拒绝播放。

答案 2 :(得分:0)

在我的情况下,外部站点上的下载链接为https://example.com/test.apk,我使用以下命令,它的工作方式类似于charm,codeignighter版本为4

return redirect()->to('https://example.com/test.apk');