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

[HELP] 文件下载问题,求助,急!!!

[复制链接]
发表于 2013-6-24 13:34:18 | 显示全部楼层 |阅读模式
下载文件,绝大部分都是损坏或者类型不对。


下载代码如下:
$data = file_get_contents($info['path']); // 读文件内容
        $name = $info['name'].'.pdf';
        $this->force_download($name, $data);

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];
                }
       $file = fopen($data,"r"); // 打开文件
                // 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));
                }
       echo fread($file,filesize($data));
    fclose($file);
        }

发表于 2013-6-24 13:39:01 | 显示全部楼层
想問個問題,
不是只要不是html及php,
用http://www.xxx.xxx/xxx.extensionname
這樣就可以下載了?

本版积分规则