這裏是所有類型的文件下載PHP代碼:
$link = 'link include file path and filename with extension'
// Grab the file extension
$extension = pathinfo($link,PATHINFO_EXTENSION);
$filename = pathinfo($link,PATHINFO_FILENAME);
// our list of mime types
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'ppt' => 'application/vnd.ms-powerpoint',
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
// Set a default mime if we can't find it
if(!isset($mime_types[$extension])) {
$mime = 'application/octet-stream';
}
else {
$mime = (is_array($mime_types[$extension])) ? $mime_types[$extension][0] : $mime_types[$extension];
}
// Generate the server headers
if(strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename='.$filename.'.'.$extension);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
} else {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: ".$mime, true, 200);
header('Content-Disposition: attachment; filename='.$filename.'.'.$extension);
header("Content-Transfer-Encoding: binary");
}
readfile($link);
對於.htaccess文件:
RewriteEngine on
RewriteCond $1 !^(index\.php|uploads|(.*)\.css|(.*)\.js|(.*)\.jpg|(.*)\.jpeg|(.*)\.png|(.*)\.bmp|(.*)\.gif|(.*)\.doc|(.*)\.shtml|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ ./index.php/$1 [L]
爲什麼你在這裏有一個while語句?這可能會返回超過1個結果。 – Augwa
數據庫表中只有一個id,沒有重複值 – Mahi
好,所以改爲改爲if語句。你看看你的PHP錯誤日誌? – Augwa