xgdd1987 发表于 2009-10-14 14:25:13

有关CI的下载辅助函数的问题

force_download($name, $data2);如果$name是中文,下载下来的文件名是乱码,该怎么解决啊??

Hex 发表于 2009-10-14 15:27:06

应该是只有 IE 下乱码吧?是 IE 的 BUG

xgdd1987 发表于 2009-10-15 14:48:49

Hex大,那该怎么解决呢?能不能告诉给我以个解决的方法。。

Hex 发表于 2009-10-15 15:49:30

我的 force_download 函数,应该在 IE6 utf-8 编码下不出乱码了,但没经过严格测试
if ( ! function_exists('force_download'))
{
        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
                @include(APPPATH.'config/mimes'.EXT);
       
                // 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] : $mimes[$extension];
                }
       
                // Generate the server headers
                if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
                {
                        header('Content-Type: "'.$mime.'"');
                        header('Content-Disposition: attachment; filename="'.iconv('utf-8', 'gb2312', $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);
        }
}

迷茫夏日 发表于 2012-12-3 10:58:35

header('Content-Disposition: attachment; filename="'.iconv('utf-8', 'gb2312', $filename).'"');
解决了我的问题,非常谢谢!!
页: [1]
查看完整版本: 有关CI的下载辅助函数的问题