3

我在下载文件时遇到了 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);
}

但不工作:(

4

3 回答 3

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); 
于 2012-08-07T16:50:55.163 回答
0

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

Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.

I tried quicktime and realplayer and still the video refuses to play.

于 2012-08-26T12:27:22.893 回答
0

in my case download link on external site is https://example.com/test.apk I use the following and it works like charm ,codeignighter version is 4

return redirect()->to('https://example.com/test.apk');
于 2020-12-08T07:41:47.923 回答