文件无法下载

时间:2017-04-04 03:55:04

标签: php .htaccess codeigniter

我正试图通过点击按钮从服务器下载我的文件。但我无法获得成功。它一次又一次地显示我的错误。这是我的小.htaccess代码和下载文件代码有错误。

  

内部服务器错误

     

服务器遇到内部错误或配置错误   无法完成您的请求。

     

请通过admin@main-hosting.eu与服务器管理员联系   告知他们此错误发生的时间以及您的行为   在此错误之前执行。

     

有关此错误的详细信息可能在服务器错误中可用   日志中。

.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

1 个答案:

答案 0 :(得分: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]