Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在chrome和IE上使用php下载word文件_Php_Download_Header_Ms Word_Readfile - Fatal编程技术网

如何在chrome和IE上使用php下载word文件

如何在chrome和IE上使用php下载word文件,php,download,header,ms-word,readfile,Php,Download,Header,Ms Word,Readfile,我在使用php头文件和readfile函数下载word文件时遇到问题。 它在Mozilla Firefox中下载时响应良好,但在使用google chrome和IE时效果不佳。请指导我解决此问题 这是我的密码: switch(strtolower($ext)) { case "doc": $ctype="application/msword"; break; case "docx": $ctype="application/vnd.openxmlform

我在使用php头文件和readfile函数下载word文件时遇到问题。 它在Mozilla Firefox中下载时响应良好,但在使用google chrome和IE时效果不佳。请指导我解决此问题

这是我的密码:

switch(strtolower($ext))
    {
        case "doc": $ctype="application/msword"; break; 
        case "docx": $ctype="application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;                       
        default: $ctype="application/octet-stream"; 
    }

    header('Content-Description: File Transfer');
    //header("Content-type: application/octet-stream");
    header("Content-Type: ".$ctype." "); 
    header('Content-Disposition: attachment; filename='.basename($rw['FINALDOCNAME']));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Pragma: private');
    header('Content-Length: ' . filesize($rw['FINALDOCNAME']));
    ob_clean();
    flush();
    readfile($filepath);
试试下面的代码

<?php
$fdl = @fopen($filePath,'rb');
header("Status: 200");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header("Pragma: hack");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".$filename."\""); 
header("Content-Transfer-Encoding: binary");
header("Content-Length:".filesize($filePath));  
if($fdl)
{
    while(!feof($fdl)) {
        print(fread($fdl, filesize($filePath)));
        flush();
        if (connection_status()!=0) 
        {
            @fclose($fdl);
            die();
        }
    }
}
?>

你检查过文件是否存在吗?为什么不同时检查filesize和readfile返回值,以确保它们符合预期。非常棒。。。这解决了我的docx腐败问题。谢谢你的发帖!
<?php
$todayDate = date('d-m-Y');
$filename = 'untitled.docx';
header('content-type: application/octet-stream');
header('content-Disposition: attachment; filename='.$filename.'-'.$todayDate.'.xls');
header('Pragma: no-cache');
header('Expires: 0');
echo file_get_contents($filename);
?>