用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3011|回复: 4
收起左侧

[讨论/交流] 有关CI的下载辅助函数的问题

[复制链接]
发表于 2009-10-14 14:25:13 | 显示全部楼层 |阅读模式
force_download($name, $data2);如果$name是中文,下载下来的文件名是乱码,该怎么解决啊??
发表于 2009-10-14 15:27:06 | 显示全部楼层
应该是只有 IE 下乱码吧?是 IE 的 BUG
 楼主| 发表于 2009-10-15 14:48:49 | 显示全部楼层
Hex大,那该怎么解决呢?能不能告诉给我以个解决的方法。。
发表于 2009-10-15 15:49:30 | 显示全部楼层
我的 force_download 函数,应该在 IE6 utf-8 编码下不出乱码了,但没经过严格测试
PHP复制代码
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][0] : $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).'"');
解决了我的问题,  非常谢谢!!

本版积分规则