PHP的xlsx头 [英] PHP xlsx header
问题描述
myphpfile.php:
<?php
header('Content-disposition:attachment; filename = myfile.pdf');
header('Content-type:application / pdf');
readfile('myfile.pdf');
?>
php文件在这里被调用并且PDF下载正常:
< a class =oglasavanjehref =../../cjenik/myphpfile.php\">下载< /a><br/ >
但这不起作用:
<?php
header('Content-disposition:attachment; filename = myfile.xlsx');
header('Content-type:application / vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('myfile.xlsx');
?>
.pdf和.xlsx文件位于同一个文件夹中。当我点击'下载'链接时,下载窗口弹出,我保存文件,但文件无法打开。它给了我以下错误:
Excel无法打开文件'myfile.xlsx',因为文件格式或
文件扩展名无效。验证该文件是否已被破坏,并且文件扩展名与该文件的格式相匹配。
文件打开很好,当我手动打开它(即没有通过链接下载)
我使用WAMP,如果这是重要的。
$
$ b ---编辑---
I'在php.net上找到了这段代码:
ob_clean();
flush();
所以最终的代码看起来像这样并且可以工作:
$ file =myfile.xlsx;
header('Content-disposition:attachment; filename ='。$ file);
header('Content-type:application / vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length:'。filesize($ file));
header('Content-Transfer-Encoding:binary');
header('Cache-Control:must-revalidate');
header('Pragma:public');
ob_clean();
flush();
readfile($ file);
感谢大家的回答。
取决于你的浏览器,许多事情会导致诸如 Content-length
, Cache-Control
etc
同时更改
内容类型
到
Content-Type
内容处置
to Content-Disposition
尝试
$ file =myfile.xlsx;
header('Content-Disposition:attachment; filename ='。$ file);
header('Content-Type:application / vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length:'。filesize($ file));
header('Content-Transfer-Encoding:binary');
header('Cache-Control:must-revalidate');
header('Pragma:public');
readfile('myfile.xlsx');
so this works:
myphpfile.php:
<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?>
that php file is called here and the PDF download works fine:
<a class = "oglasavanje" href = "../../cjenik/myphpfile.php">download</a><br/>
but this doesn't work:
<?php
header('Content-disposition: attachment; filename=myfile.xlsx');
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('myfile.xlsx');
?>
both .pdf and .xlsx files are in the same folder. When I click on the 'download' link the download windows pops up, I save the file, but the file can't be opened. It gives me the following error:
Excel cannot open the file 'myfile.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
The file opens fine when I open it manually (i.e. without downloading it via the link)
I'm using WAMP, if that's of importance.
Any help is appreciated.
--- EDIT ---
I've found this piece of code on php.net:
ob_clean();
flush();
so the final code looks like this and it works:
$file = "myfile.xlsx";
header('Content-disposition: attachment; filename='.$file);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($file);
Thanks everyone for the answers.
Depends on your browsers so many things can cause such behavior such as Content-length
, Cache-Control
etc
Also Change
Content-type
to Content-Type
Content-disposition
to Content-Disposition
Try
$file = "myfile.xlsx" ;
header('Content-Disposition: attachment; filename=' . $file );
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('myfile.xlsx');
这篇关于PHP的xlsx头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!