您的位置:首頁技術文章
文章詳情頁

php文件下載后無法打開的處理方案及代碼

瀏覽:14日期:2022-09-06 16:39:11

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文件下載后無法打開的處理方案及代碼的文章就介紹到這了,更多相關php文件下載后無法打開的解決內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: PHP
相關文章:
国产综合久久一区二区三区