2012-08-07 69 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,仍然視頻拒絕播放。