2011-04-24 64 views
2

這涉及下載文件用PHPPHP圖片文件下載

我的PHP版本是5.3.5和我的Apache 2.2.17是

我想下載中心文件(PDF,JPG,TIFF)我已經上傳到我的服務器中,並且他們以相同的大小和類型下載,但我看不到它們。我猜他們沒有被複制的權利。但是,當我打開原來上傳的,他們工作得很好。 我已經看到幾乎所有的問題出現建議,他們都沒有回答這個問題,只有類似的是this,但仍然不能回答我的問題。

下載我使用這個代碼

 header("Content-type: application/force-download"); 
     header('Content-Disposition: inline; filename="' . $dir . '"'); 
     header("Content-Transfer-Encoding: Binary"); 
     header("Cache-Control: no-store, no-cache, must-revalidate"); 
     header("Cache-Control: post-check=0, pre-check=0", false); 
     header("Pragma: no-cache"); 
     header("Content-length: ".filesize($dir)); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename="' . $file . '"'); 
     readfile("$dir"); 

其中$dir="62756d616769636e63/646973736572746174/ehddggh/1.JPG"$file="1.JPG"

誰能給我一個暗示什麼,我做錯了,或者給我一個更好的解決方案,以下載文件?

+0

'內容type'兩次...'內容disposition'兩次。第一個頭部不會做anythnig(不會被髮送)。'文件大小($ dir)'也有點奇怪,你不覺得嗎? A ** dir **的**文件**大小... – Rudie 2011-04-25 00:33:43

回答

1

謝謝大家的答案。我把下載文件作爲一個函數調用到其他函數中,所以最後除了其他文件之外,我不得不單獨編寫一個腳本。我的問題是,我需要它是安全的,只有下載文件,如果它屬於用戶和用戶登錄,所以我發送所有我需要的數據,加密和腳本內我使用一系列的東西看看擁有者是否真的是登錄用戶。所以如果有人想知道這是我使用的代碼,並且完美地工作。

<?php 
session_start(); 
$a=$_GET['a']; 
$parts=explode("-",$a); 
$tres=$parts[0]; 
$nombre=$partes[1]; 
$dbcodletra=substr($tres,2); 
if($dbcod!=$_SESSION["username"])$boolt=0; 
if($ext==1) $extl=".jpg"; 
if($ext==2) $extl=".jpeg"; 
if($ext==3) $extl=".tif"; 
if($ext==4) $extl=".tiff"; 
if($ext==5) $extl=".pdf"; 
if($ext==6) $extl=".doc"; 
if($ext==7) $extl=".docx"; 
if($tipoproy==1) $dir="rute/".$dbcodletra."/".$nombre.$extl; 
if($tipoproy==2) $dir="rute/".$dbcodletra."/".$nombre.$extl; 
if($tipoproy==3) $dir="rute/".$dbcodletra."/".$nombre.$extl; 
if($tipoproy==4) $dir="rute/".$dbcodletra."/".$nombre.$extl; 
if (file_exists($dir) && $boolt) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($dir)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($dir)); 
    ob_clean(); 
    flush(); 
    readfile($dir); 
    exit; 
}else echo "<meta http-equiv=\"Refresh\" content=\"0;url=misdocumentos.php\">"; 
?> 
+0

加入ob_clean()和flush()就像魅力一樣。 – Yogesh 2016-04-10 19:39:17

1
readfile($dir); // without the quotes? 

同時,確保$目錄確實存在

is_file($dir)file_exists($dir)

+0

雖然沒有引號將其寫得更乾淨,但這不是他的問題的原因 – 2011-04-24 23:08:54

+0

刪除所有這些標題並查看是否獲得任何輸出在所有。 – Halcyon 2011-04-24 23:11:41

3

這聞起來像你得到你下載的文件額外的(假)的內容。

確保您的文件中的PHP打開標籤前沒有BOM標題,空格或其他任何內容;此外,在關閉PHP標記後(如果關閉PHP標記),您沒有尾隨空格或任何其他數據。

此外,清理你的代碼有點:爲什麼多個Content-Type標題?爲什麼多個Content-Disposition標題?

0

你的頭顯得凌亂,只是嘗試這樣做:

header('Pragma: public'); 
header('Cache-Control: public, no-cache'); 
header('Content-Type: application/octet-stream'); 
header('Content-Length: ' . filesize($dir)); 
header('Content-Disposition: attachment; filename="' . basename($dir) . '"'); 
header('Content-Transfer-Encoding: binary'); 

readfile($dir); 
1

支付它向前一兩歲的問題...

我曾與腐敗下載一個類似的問題,可能無法打開(另存爲完美的工作時,用鼠標右鍵單擊&)。在閱讀@Jon's答案後,我想他正在做些什麼。如果您查看readfile的文檔(鏈接如下),您將在其示例中看到ob_clean(),flush()和exit。所有這些將最大限度地減少響應中額外字符數據的泄漏。我剛剛複製了他們的示例#1,我的問題解決了。

http://php.net/readfile