php文件下载后无法打开的处理方法

发布时间:2021-07-19 13:46:42 作者:chen
来源:亿速云 阅读:192

本篇内容介绍了“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文件下载后无法打开的处理方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Outlook客户端配置exchange后无法打开
  2. Bootstrap导航菜单点击后无法自动添加active的处理方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:Centos7如何安装Chacha20加密算法

下一篇:python中的EasyOCR库是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》