PHP - CodeIgniter 下载远程文件


PHP - CodeIgniter download remote file

我在下载文件时使用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);
}

但不起作用:(

尝试 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); 

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

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

我尝试了快速时间和实时播放器,但视频仍然拒绝播放。

在我的

情况下,外部网站上的下载链接https://example.com/test.apk我使用以下方法,它的工作方式就像魅力一样,codeignighter版本是4

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