PHP下载图片后文件打开显示损坏问题
用php写个图片下载方法,测试发现下载的图片大小都没问题,但是无法打开文件。
解决方法如下:
首先打开文件下载代码,增加
?
1
2
|
ob_clean();
flush ();
|
完整下载图片代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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格式一定要和后缀名保存一直,否也会提示格式错误或者文件被破坏
代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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文件下载后无法打开的解决内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.py.cn/php/jiaocheng/31677.html
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 74
-
2025-05-29 30
-
2025-05-27 53
-
2025-05-29 83
-
2025-05-25 17
热门评论