2017-04-04 85 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