2016-03-05 27 views
0

夥計。 我正在使用laravel框架的本地服務器並嘗試下載文件。 我嘗試了很多情況,但沒有爲我工作。如何解決在PHP下載的問題?

事情是我沒有得到任何錯誤。

下載wont`t開始,但在響應我有這樣的事情:

� 

���JFIF��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 95��C  
���}!1AQa"q2���#B��R��$3br� 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������  
���w!1AQaq"2�B���� #3R�br� 
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?���!�A���I��+�P�sB���i��?�Y�L��iG���B���Ap�����J;���;}ic=F{R�Z�� 
�$��z�q��~�p8�h�[email protected]��;����t�@�Fx��Ǯq�S#<������qS- 

這是我用: 試過這樣:

$file = 'D:\Server\OpenServer\domains\memo.ru\public\img\avatars\\'.$file_name; 
$filePath=$file; 

這:

$file = "http://example.com/img/avatars/1457091259.png"; 

1.

set_time_limit(0); 
    $file = @fopen($filePath, 'rb'); 
    while (!feof($file)) { 
     print(@fread($file,1024*8)); 
     //ob_flush(); 
     flush(); 
    } 
$handle = fopen($filePath, 'rb'); 
    $buffer = ''; 
    while (!feof($handle)) { 
     $buffer = fread($handle, 4096); 
     echo $buffer; 
     ob_flush(); 
     flush(); 
    } 
    fclose($handle); 
header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    //header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
$real_path = 'D:\Server\OpenServer\domains\example.com\public\img\avatars\\'; 
$file = "http://example.com/img/avatars/1457091259.png"; 

$url = 'http://example.com/img/avatars/'.$tmpFileName; 
$path = $real_path.$tmpFileName; 

$fp = fopen($path, 'w'); 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_FILE, $fp); 

$data = curl_exec($ch); 

curl_close($ch); 
fclose($fp); 

回答

0

如果你使用Laravel 5那麼這將是最直接的方法:

public function download() 
{ 

    $file = base_path(). "public/img/avatars/1457091259.png"; 

    $name = "Human Name For File.jpg"; 

    $headers = ['Content-Type: application/octet-stream']; 

    return Response::download($file, $name, $headers); 
} 

僅舉確保您的class前頂部有use Response

application/octet-stream標頭應該強制瀏覽器下載文件,而不是將其顯示在瀏覽器窗口中。

+0

我有「使用響應」。瀏覽器不會開始下載。也許有其他設置? –

+0

您在瀏覽器中訪問哪個URL並顯示什麼錯誤? – Joseph

+0

沒有錯誤。就這個(i.yapx.ru/yO0.png)作爲迴應。 –

0

這應該工作

header('Content-Type: image/png'); 
header('Content-disposition: attachment; filename='.$fileName); 
echo readfile($file); 
die(); 
+0

相同的問題:http://i.yapx.ru/yO0.png –

+0

$ file ='https://assets.easypost.com/img/carriers/tnt-logo-ca.png'; header('Content-Type:image/png'); header('Content-disposition:attachment; filename = test.png'); echo readfile($ file); die();這段代碼當然適用於我在.php文件中設置它並使用http://localhost/demo.php調用該代碼 – Indrajit

0

我已經非常成功地使用了以下內容來下載/發送文件 - 這可能對您有用嗎?

這樣稱呼它:

call_user_func('send_file', 'myfile.docx', 'c:/temp/document.docx'); 


function send_file($name, $strPath) { 
    if(file_exists($strPath)) { 
     if(!is_file($strPath) or connection_status()!=0) { return FALSE; } 

     header("Cache-Control: no-store, no-cache, must-revalidate"); 
     header("Cache-Control: post-check=0, pre-check=0", false); 
     header("Pragma: no-cache"); 
     header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT"); 
     header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); 
     header("Content-Type: application/octet-stream"); 
     header("Content-Length: ".(string)(filesize($strPath))); 
     header("Content-Disposition: inline; filename={$name}"); 
     header("Content-Transfer-Encoding: binary\n"); 

     if($file = fopen($strPath, 'rb')) { 
      while(!feof($file) and (connection_status()==0)) { 
       print(fread($file, 1024*8)); 
       flush(); 
      } 
      fclose($file); 
     } 
     clearstatcache(); 
     return((connection_status()==0) and !connection_aborted()); 
    } 
}