2011-12-08 44 views
-2

我現在有當前代碼:強制下載提示PHP碎文件

if($_POST['mode']=="save") { 
    $path = $_POST['path']; 
    $file = end(explode('/', $path)); 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header("Content-Disposition: attachment; filename=$file"); 
    readfile($file); 
} 

我做的是什麼,得到該文件的保存路徑,它爆炸僅文件名並嘗試保存該文件。我得到保存爲提示,但是當我保存它並嘗試打開文件時,我得到了文件/圖像損壞的錯誤信息。

這些文件不保存在home目錄中,而是保存在一個名爲uploads的子目錄中。

有人知道我做錯了什麼?

在此先感謝。 思南

+0

這是用於保存文件的代碼?你確定該文件被正確保存嗎?在讀取文件之前,您是否嘗試將readfile()放入is_readable(){...}中? – macjohn

+0

@macjohn我只注意到我的文件不可讀。爲什麼不? – Sino

+0

is_readable()如果filename指定的文件或目錄存在且可讀,則返回TRUE,否則返回FALSE。您的文件不存在,或者您沒有閱讀權限。 – macjohn

回答

0

我通常使用的是這樣的:

$siteEmail = '[email protected]'; 
$companyName = 'My Company'; 

# Path from server root 
$fileDirectory = '/home/uploads/'; // include trailing slash 

// get the file reference 
$passedFile = $_POST['path']; 
$filename = @urldecode($passedFile); // useful if your file is sent by $_GET 
if((!isset($passedFile))||(@$passedFile=='')) { 
    echo "<html><title>".$companyName." - Download File</title><body>ERROR: Please pass a valid filename.<br /><br />Please go back and try again. If you continue to see this message please contact <a href=\"mailto:".$siteEmail."\">".$siteEmail."</a></body></html>";exit(); 
} 

// required for IE, otherwise Content-disposition is ignored 
if(ini_get('zlib.output_compression')) 
    ini_set('zlib.output_compression', 'Off'); 

$file_extension = strtolower(substr(strrchr($filename,"."),1)); 

if($filename == "") { 
    echo "<html><title>".$companyName." - Download File</title><body>ERROR: download file NOT SPECIFIED.<br /><br />Please go back and try again. If you continue to see this message please contact <a href=\"mailto:".$siteEmail."\">".$siteEmail."</a></body></html>"; 
    exit; 
} elseif (! file_exists($fileDirectory . $filename)) { 
    echo "<html><title>".$companyName." - Download File</title><body>ERROR: File not found.<br /><br />Please go back and try again. If you continue to see this message please contact <a href=\"mailto:".$siteEmail."\">".$siteEmail."</a> 
    <p>path: ".$fileDirectory.$filename."</p> 
    <p>DOCUMENT_ROOT: ".$_SERVER['DOCUMENT_ROOT']."</p> 
    </body></html>"; 
    exit; 
}; 

switch($file_extension) { 
    case "pdf": $ctype="application/pdf"; break; 
    case "exe": $ctype="application/octet-stream"; break; 
    case "zip": $ctype="application/zip"; break; 
    case "doc": $ctype="application/msword"; break; 
    case "xls": $ctype="application/vnd.ms-excel"; break; 
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
    case "rtf": $ctype="application/rtf"; break; 
    case "gif": $ctype="image/gif"; break; 
    case "png": $ctype="image/png"; break; 
    case "wav": $ctype="audio/wav"; break; 
    case "mp3": $ctype="audio/mpeg3"; break; 
    case "wmv": $ctype="video/x-ms-wmv"; break; 
    case "avi": $ctype="video/avi"; break; 
    case "asf": $ctype="video/x-ms-asf"; break; 
    case "mpg": $ctype="video/mpeg"; break; 
    case "mpeg": $ctype="video/mpeg"; break; 
    case "jpeg": 
    case "jpg": $ctype="image/jpg"; break; 
    default: $ctype="application/force-download"; 
} 

// Everything went fine - you could log the download in the databse here if required? 

// OUTPUT THE FILE 

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // cache stop 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // cache stop 
header("Cache-Control: must-revalidate"); // cache stop 
header("Content-Type: $ctype"); // output filetype 
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($fileDirectory.$filename)); 
readfile($fileDirectory.$filename); 
exit();