PHP文件请求/下载脚本编码为text/html

PHP文件请求/下载脚本编码为text/html,php,pdf,http-headers,Php,Pdf,Http Headers,我对这个文件下载脚本有问题。它接收ID,查询文件名,查找扩展名,并根据MIME类型设置适当的头 问题是,我认为在本例中,标题被设置为text/html,而不是application/PDF。它以前是有效的,现在没有那么多。我尝试了一个简单的强制下载脚本(),它确实有效。有人知道这是为什么吗 这是我正在使用的代码。(注意:此代码是继承的。正在尝试修复此问题。) 为什么要使用文件扩展名来确定mime类型?有fileinfo()可以帮你做到这一点。文件扩展名可能存在漏洞。fileinfo()查看实际的

我对这个文件下载脚本有问题。它接收ID,查询文件名,查找扩展名,并根据MIME类型设置适当的头

问题是,我认为在本例中,标题被设置为text/html,而不是application/PDF。它以前是有效的,现在没有那么多。我尝试了一个简单的强制下载脚本(),它确实有效。有人知道这是为什么吗

这是我正在使用的代码。(注意:此代码是继承的。正在尝试修复此问题。)


为什么要使用文件扩展名来确定mime类型?有
fileinfo()
可以帮你做到这一点。文件扩展名可能存在漏洞。fileinfo()查看实际的文件内容以确定它们是什么。同样,在输出header()调用之前,不应该关闭输出缓冲。您可能会因无意中产生输出而禁用它们。我可以肯定地说,该文件是pdf文件,MIME类型是application/pdf。同样,我没有把这段代码放在一起,但我确实需要修复它。我将尝试用fileinfo()替换文件扩展名/MIME位。是否有其他东西会导致文本/html而不是应用程序/pdf编码?当然。如果header()调用失败是因为您已经有了输出,那么php将默认为text/html。e、 g.检查您的日志中是否有“无法发送头-输出已在第XXX行发送”类型的消息。我只发现以下内容:[Tue Jan 21 14:44:18 2014][warn][client xx.XXX.xx.xx](104)对等方重置连接:mod_fcgid:从FastCGI服务器读取数据时出错[Tue Jan 21 14:44:18][error][client xx.XXX.xx]脚本头过早结束:articles.php
<?php 
include("includes/function_lib.php");
//*******************************************************************************//
//*******************************************************************************//
// Download Code
    function output_file($file, $name, $mime_type='')
    {
        /*This function takes a path to a file to output ($file), 
        the filename that the browser will see ($name) and 
        the MIME type of the file ($mime_type, optional).
        If you want to do something on download abort/finish,
        register_shutdown_function('function_name');
        */
        if(!is_readable($file)) die('File not found or inaccessible!');

        $size = filesize($file);
        $name = rawurldecode($name);

        /* Figure out the MIME type (if not specified) */
        $known_mime_types=array(
        "pdf" => "application/pdf",
        "sql" => "text/plain",
        "txt" => "text/plain",
        "html" => "text/html",
        "htm" => "text/html",
        "exe" => "application/octet-stream",
        "zip" => "application/zip",
        "doc" => "application/msword",
        "xls" => "application/vnd.ms-excel",
        "ppt" => "application/vnd.ms-powerpoint",
        "gif" => "image/gif",
        "png" => "image/png",
        "jpeg"=> "image/jpg",
        "jpg" => "image/jpg",
        "php" => "text/plain"
        );

        if($mime_type=='')
        {
            $file_extension = strtolower(substr(strrchr($file,"."),1));
            if(array_key_exists($file_extension, $known_mime_types))
            {
                $mime_type=$known_mime_types[$file_extension];

            } 
            else 
            {
                $mime_type="application/force-download";
            }
        }

        @ob_end_clean(); //turn off output buffering to decrease cpu usage

        // required for IE, otherwise Content-Disposition may be ignored
        if(ini_get('zlib.output_compression'))
        ini_set('zlib.output_compression', 'Off');

        header('Content-Type: ' . $mime_type);
        header('Content-Disposition: attachment; filename="'.$name.'"');
        header("Content-Transfer-Encoding: binary");
        header('Accept-Ranges: bytes');

        /* The three lines below basically make the 
        download non-cacheable */
        header("Cache-control: private");
        header('Pragma: private');
        header("Expires: Mon, 5 Jun 2019 05:00:00 GMT");

        // multipart-download and download resuming support
        if(isset($_SERVER['HTTP_RANGE']))
        {
        list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
        list($range) = explode(",",$range,2);
        list($range, $range_end) = explode("-", $range);
        $range=intval($range);
        if(!$range_end) {
        $range_end=$size-1;
        } else {
        $range_end=intval($range_end);
        }

        $new_length = $range_end-$range+1;
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: $new_length");
        header("Content-Range: bytes $range-$range_end/$size");
        } else {
        $new_length=$size;
        header("Content-Length: ".$size);
        }

        /* output the file itself */
        $chunksize = 3*(1024*1024); //you may want to change this
        $bytes_send = 0;
        if ($file = fopen($file, 'r'))
        {
        if(isset($_SERVER['HTTP_RANGE']))
        fseek($file, $range);

        while(!feof($file) && 
        (!connection_aborted()) && 
        ($bytes_send<$new_length)
        )
        {
        $buffer = fread($file, $chunksize);
        print($buffer); //echo($buffer); // is also possible
        flush();
        $bytes_send += strlen($buffer);
        }
        fclose($file);
        } else die('Error - can not open file.');

        die();
    }   

$data_action    = $_REQUEST['action'];

if($data_action == 'downloadfile') {
    if(isset($_REQUEST['file'])) {
        $file = $_REQUEST['file'];
    } else {
        $Id = $_REQUEST["id"];
        $sqlNewsletter = "select  case_file from tbl_case where case_id=$Id and deleteflag='active' and status='active'"; 
        $rsNewsletter  = mysql_query($sqlNewsletter);
        if(mysql_num_rows($rsNewsletter) > 0) {
            $rowNewsletter = mysql_fetch_object($rsNewsletter);
            $file = $rowNewsletter->case_file;
        }
    }
    $filename  = explode('/', $file);
    $name      = $filename[2];
    //$name = $file;
    $result = output_file($file, $file);
//}
?>

<?php
    //$result = $s->output_file($file, $name);
    //$s->pageLocation("index.php?pagename=database_backup&action=done&result=$result"); 
}
?>