2017-04-04 215 views
0

我想下載我的文件從服務器按鈕點擊。但我無法取得成功。它一次又一次地顯示我的錯誤。這裏是我的小小的.htaccess代碼和下載文件的錯誤代碼。文件無法下載

內部服務器錯誤

服務器遇到一個內部錯誤或配置錯誤, 無法完成您的請求。

請通過[email protected]至 與服務器管理員聯繫,通知他們發生此錯誤的時間以及您在此錯誤發生前執行的操作 。

有關此錯誤的更多信息可能在服務器錯誤 日誌中可用。

的.htaccess代碼是在這裏下載文件

DirectoryIndex index.php 

RewriteEngine on 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond $1 !^(index\.php|robots\.txt) 

RewriteRule ^(.*)$ index.php?/$1 [L] 

代碼。

$query = "Select * from table_name where id = '".$id."'"; 

      $sql = mysqli_query($con, $query); 

      while($row = mysqli_fetch_array($sql)){ 

       $path = $row['File_path'];    
       header('content-Disposition: attachment; filename = '.$path.''); 
       header('content-type:Documents/content=pdf'); 
       header('content-length'.filesize($path)); 
       readfile($path); 

我提到這個問題,但它說刪除.htaccess文件,但我想保留這個文件。

Referral Link

+0

爲什麼你在這裏有一個while語句?這可能會返回超過1個結果。 – Augwa

+0

數據庫表中只有一個id,沒有重複值 – Mahi

+0

好,所以改爲改爲if語句。你看看你的PHP錯誤日誌? – Augwa

回答

0

這裏是所有類型的文件下載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] 
+0

Thorks Gaurav此代碼完美。 – Mahi