2014-12-07 223 views
0

目前我正在使用下面的php代碼來下載文件。Php文件下載問題?

PHP代碼:

<?php 
timeout(); 
//require_once("config.php"); 
if(!isset($_SESSION['front_username']) && isset($_SESSION['front_username']) == "" && 
    !isset($_SESSION['front_password']) && isset($_SESSION['front_password']) == "" && 
    !isset($_SESSION['user_id']) && isset($_SESSION['user_id']) == "") { 
    header("Location:login.php"); 
    exit(); 
} 
$file_get = $_GET['filename']; 
$tmp = explode(".",$file_get); 
$file_name1 = substr($file_get, 33);  
switch ($tmp[count($tmp)-1]) { 
    case "pdf": $ctype="application/pdf"; break; 
    case "exe": $ctype="application/octet-stream"; break; 
    case "zip": $ctype="application/zip"; break; 
    case "docx": 
    case "doc": $ctype="application/msword"; break; 
    case "csv": 
    case "xls": 
    case "xlsx": $ctype="application/vnd.ms-excel"; break; 
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
    case "gif": $ctype="image/gif"; break; 
    case "png": $ctype="image/png"; break; 
    case "jpeg": 
    case "jpg": $ctype="image/jpg"; break; 
    case "tif": 
    case "tiff": $ctype="image/tiff"; break; 
    case "psd": $ctype="image/psd"; break; 
    case "bmp": $ctype="image/bmp"; break; 
    case "ico": $ctype="image/vnd.microsoft.icon"; break; 
    default: $ctype="application/force-download"; 
} 
header("Pragma: public"); // required 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: $ctype"); 
header("Content-Disposition: attachment; filename=\"".$file_name1."\";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize("upload_doc/$file_get")); 
ob_clean(); 
flush(); 
readfile("upload_doc/$file_get"); 
?> 

它的下載正確,如果上傳的文件名是字母。但是,如果上傳的文件名具有該文本,例如my doC#2.docdev & developer.doc,則它正在下載但未顯示。在我下載的文件夾中,我看到my doc

我不明白我的代碼有什麼問題,有人可以告訴我嗎?

+1

我看到的第一個問題是在這一行** $ file_name1 = subs tr($ file_get,33); **您將從文件名中截斷前32個字符。爲什麼這樣? – Diptendu 2014-12-07 10:57:41

+0

@Diptendu例如:原始上傳的文件名重命名爲:'35dbde7e8eab023248adf616747caf6e-bind candidate#2.doc',當我下載它時,我只想顯示用戶上傳的名稱。 – creativeartbd 2014-12-07 11:02:25

+0

好的。所以你想刪除MD5部分。得到它了。謝謝!! – Diptendu 2014-12-07 11:04:07

回答