php文件下载后无法打开的处理方案及代码

在本篇内容里小编给大家整理的是一篇关于php文件下载后无法打开的处理方案及代码内容,有兴趣的朋友们可以学习下。

PHP下载图片后文件打开显示损坏问题

用php写个图片下载方法,测试发现下载的图片大小都没问题,但是无法打开文件。

解决方法如下:

首先打开文件下载代码,增加

 ob_clean(); flush();

完整下载图片代码

 if(isset($_GET['action'])&&$_GET['action'] == 'download') { if($_GET['file']) { $fileinfo = pathinfo($_GET['file']); header('Content-type: application/x-'.$fileinfo['extension']); header('Content-Disposition: attachment; filename=favicon.ico'); ob_clean(); flush(); readfile($_GET['file']); exit(); } }

内容扩展:

php下载excel文件,

1、在下载的过程中不要 输出任何非文件信息,比如 echo log信息。 否则下载后的文件无法打开,提示格式错误或者文件被破坏。

2、 输出的excel格式一定要和后缀名保存一直,否也会提示格式错误或者文件被破坏

代码如下:

 if (file_exists(CACHE_PATH . $file_name)){ //$this->logger->error('file realpath:'.realpath(CACHE_PATH . $file_name)); header( 'Pragma: public' ); header( 'Expires: 0' ); header( 'Content-Encoding: none' ); header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); header( 'Cache-Control: public' ); header( 'Content-Type: application/vnd.ms-excel'); header( 'Content-Description: File Transfer' ); header( 'Content-Disposition: attachment; filename=' . $file_name ); header( 'Content-Transfer-Encoding: binary' ); header( 'Content-Length: ' . filesize ( CACHE_PATH . $file_name ) ); readfile ( CACHE_PATH . $file_name ); } else { $this->logger->error('export model :'.$id.' 错误:未生产文件'); echo ''; }

以上就是php文件下载后无法打开的处理方案及代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » PHP编程