Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
PHP下载在输出屏幕上显示乱码_Php_Excel_File_Download - Fatal编程技术网

PHP下载在输出屏幕上显示乱码

PHP下载在输出屏幕上显示乱码,php,excel,file,download,Php,Excel,File,Download,PHP的文件下载代码在屏幕上显示乱码,而不是打开保存/打开窗口。我从PHP.net网站上得到了这段代码,请帮助我该怎么做 $file1='DataUpload.xls' if (file_exists($file1)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposit

PHP的文件下载代码在屏幕上显示乱码,而不是打开保存/打开窗口。我从PHP.net网站上得到了这段代码,请帮助我该怎么做

$file1='DataUpload.xls'

if (file_exists($file1)) {
      header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename='.basename($file1));
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file1));
      ob_clean();
      flush();
      readfile($file1);
      exit;
    }
屏幕上的输出:
��������>������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ f2�ɀ����\p B�A.�=���=��G9!8X@�"��1.���口径1���口径1���口径1���口径1����阿里亚1���口径1� ��口径1���口径1�4.��口径1� ��口径1���口径1���口径1,8��口径18��口径1�8.��口径1�>��口径1�4.��口径1�这是正确的行为。
readfile()
将文件内容输出到输出缓冲区。由于您的文件是Excel5二进制格式,它会像在文本编辑器中打开.xls文件一样输出您在上面看到的乱七八糟的内容。您试图实现什么?将mimetype更改为您正在下载的文件的正确值。@leemo iam正在尝试打开/保存此文件给我们er本地系统,但如果我删除read($file1)我没有得到任何输出。@MikeW我应该为值更改什么,您能举个例子吗。完整的mime类型列表由IANA维护。找到与您的文件匹配的类型。我更改了代码,但仍然得到与gibbrish相同的结果。这有什么原因吗?可能是因为firefox或内存问题吗?它与我的服务器。我使用的是FireFox最新版本。请尝试清除缓存,关闭FF的所有实例,然后重新打开。这是一个很好的代码。我已重新启动服务器和计算机,并从计算机上清除了tmp文件,仍然无法运行,问题相同。我正在xampp服务器上运行它。有任何问题吗?打开错误报告,查看是否引发了任何错误,如h标题已发送。在发送标题之前,您可能在某处有一个前导或尾随空格。此外,我在$file1='DataUpload.xls'后面没有看到分号
$file1 = 'DataUpload.xls';

    header('Pragma: public');   // required
    header('Expires: 0');       // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false);
    header('Content-Type:application/force-download');
    header('Content-Disposition: attachment; filename="'.basename($file1).'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file1));    // provide file size
    header('Connection: close');
    readfile($file1);       
    exit();