douduan6731 2019-01-16 14:42 采纳率: 0%
浏览 103

Zlib打破了CodeIgniter Zip Downloader和PHPExcel

I'm facing an issue where if Zlib compression is enabled on the web server, downloading any zip file using CodeIgniter's force_download function in the system's download_helper.php file breaks the archive file and prevents users from opening it.

enter image description here

Here is the force_download function for zipped files after compressing XLSX files in it:

    function force_download($filename = '', $data = '')
    {
        if ($filename == '' OR $data == '')
        {
            return FALSE;
        }

        // Try to determine if the filename includes a file extension.
        // We need it in order to set the MIME type
        if (FALSE === strpos($filename, '.'))
        {
            return FALSE;
        }

        // Grab the file extension
        $x = explode('.', $filename);
        $extension = end($x);

        // Load the mime types
        if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php'))
        {
            include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');
        }
        elseif (is_file(APPPATH.'config/mimes.php'))
        {
            include(APPPATH.'config/mimes.php');
        }

        // Set a default mime if we can't find it
        if ( ! isset($mimes[$extension]))
        {
            $mime = 'application/octet-stream';
        }
        else
        {
            $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
        }

        // Generate the server headers
        if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)
        {
            header('Content-Type: "'.$mime.'"');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Content-Transfer-Encoding: binary");
            header('Pragma: public');
            header("Content-Length: ".strlen($data));
        }
        else
        {
            header('Content-Type: "'.$mime.'"');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header("Content-Transfer-Encoding: binary");
            header('Expires: 0');
            header('Pragma: no-cache');
            header("Content-Length: ".strlen($data));
        }

        exit($data);
    }

Enabling Zlib also breaks PHPExcel by producing garbled characters in Excel files as you can see in this image.

enter image description here

PHPExcel download function for a single XLSX file. Here's the gist for the entire Excel generator, zip, and downloader functions https://gist.github.com/TheWebAuthor/5773e56086df4317c7cf54aab45df328

ob_end_clean();                    
header('Content-Description: File Transfer');                            
header('Content-Type: application/vnd.ms-excel');                            
header('Content-Disposition: attachment; filename=' .$fileinfo['filename'] . '_' . $customerId . '_' . date("mdy") . '.xls');              
header('Content-Transfer-Encoding: binary');                            
header('Expires: 0');                            
header('Cache-Control: must-revalidate');                            
header('Pragma: public');                            
header('Content-Length: ' . filesize($file));                            
ob_clean();                            
flush();                            
readfile($file);

Is there a proper workaround to enable Zlib and not breaking these features?

  • 写回答

1条回答 默认 最新

  • douluan5523 2019-01-29 21:16
    关注

    a) application/vnd.ms-excel is for XLS BIFF8 format; for XLSX use:

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    

    b) either the ZIP file might not be properly packed or $mimes[$extension] might not have application/zip. difficult to tell without having seen the function which packs it, the response headers, and eventually the file header of the current output.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题