2012-08-07 71 views
3

我有在下载文件与笨麻烦..PHP - 笨下载远程文件

的代码完全在单独的PHP文件,但是当我在笨把代码: 文件将下载成功,但它是损害:(

注:我在工作与视频文件从远程服务器

代码:

$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); 
} 

但不工作:(

+0

问题是什么?什么不工作?任何错误消息? [你有什么尝试?](http://www.whathaveyoutried.com) – orourkek 2012-08-07 16:20:24

+1

CodeIgniter [输出类](http://codeigniter.com/user_guide/libraries/output.html)的责备!参见< - 该页面。 我不知道如何解决问题,但这是问题所在。 – Prasanth 2012-08-07 16:54:19

+0

你用gzip压缩输出吗?如果是这样,那是你的问题。如果启用gzip压缩,则无法直接从控制器方法输出内容。你必须使用一个视图。 – Brendan 2012-08-07 17:08:15

回答

4

尝试force_download()帮手笨的,看看它是否有助于

force_download($name, $data); 

UPDATE

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

$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); 
+0

这可能不是因为他想专门利用ob_flush()属性。 force_download是否允许流式传输是现在的问题。 – Prasanth 2012-08-07 16:55:42

+0

不工作..文件下载,但文件不工作..我使用远程文件,不是本地! – MAMProgr 2012-08-07 17:43:16

+2

@MAMProgr - 您需要提供数据而不是网址 - 请参阅我的更新 – TigerTiger 2012-08-08 07:26:09

0

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

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

我试过quicktime和realplayer,仍然视频拒绝播放。