ThinkPhp文件下载问题
来源:互联网 发布:日本殖民地知乎 编辑:程序博客网 时间:2024/05/16 05:38
作者:宋世超 421271944@qq.com [有问题请联系作者]
去TP的官网问了,也没人回答,以后有问题还是自己解决好了,去群里问问还行。
另外这有几篇文章,很有参考价值。大家一定要看看。
http://www.iprue.com/article/62/
http://blog.myspace.cn/e/400011295.htm
http://hi.baidu.com/xuelongpaox/blog/item/ce241983e36355ae0cf4d296.html
http://sunmoon.kmblog.info/?p=42
<!-- 最终使用的版本 -->
最近项目中用到了下载文件的功能,用TP的内置类写好了,但是出现这样一个问题,
点击下载后,文件内容直接输出到浏览器上了,不出下载提示框,找了很长时间,
保证程序没有错误。找啊找,两个周试过了所有能想到的办法,最后我认为原因是
服务器的问题,我在自己的机器上用wamp作服务器,就出下载提示框,用我们的服务器
AppServ做服务器就不行。后来改了Http里面的头信息,就好用了。内容如下:
//发送Http Header信息 开始下载
header("Content-Type:text/html; charset=utf-8");
header("Pragma: public");
header("Expires: 0"); // set expiration time
//header("Content-Type: application/force-download"); //告诉浏览器强制下载
header("Content-Transfer-Encoding: binary");
header("Cache-control: private");
header("Pragma: no-cache" ); //不缓存页面
header("Cache-Component: must-revalidate, post-check=0, pre-check=0" );
header("Content-Disposition: attachment; filename=".iconv('UTF-8','GB2312//TRANSLIT',$showname));
//header("Content-Disposition: attachment; filename=".$showname);
header("Content-Length: ".$length);
header("Content-type: ".$type);
header('Content-Encoding: UTF-8');
header("Content-Transfer-Encoding: binary" );
//下面一行就是改动的地方
//即用iconv("UTF-8″,"GB2312//TRANSLIT",$showname)系统函数转换编码为gb2312
//header("Content-Disposition: attachment; filename=".iconv('UTF-8','GB2312//TRANSLIT',$showname));
使用Http这个内置类的时候,需要修改里面的header头信息。
public function download( ){
import("ORG.Net.Http");
$uploadPath = "C:\AppServ\www\\teach\\teach\upload\\"; //设置文件上传路径,服务器上的绝对路径
$id = $_GET['id']; //GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。
if($id == "") //如果id为空而出错时,程序跳转到项目的Index/index页面。或可做其他处理。
{
echo '<script language="javascript">alert("没有此文件");</script>';
}
$file = D("fileView"); //利用与表file对应的数据模型类FileModel来建立数据对象。
$map = array( 'file.id' => $id );
$result = $file->where($map)->find(); //根据id查询到文件信息
//dump($result);
if($result == false) //如果查询不到文件信息而出错时,程序跳转到项目的Index/index页面,或可做其他处理
{
$this->redirect('index');
}else {
$savename = $result['newname']; //文件保存名
$showname = $result['oldname']; //文件原名
$filename = $uploadPath.$savename; //完整文件名(路径加名字)
Http::download($filename,$showname);
}
}
public function download4(){
$uploadPath = 'C:\AppServ\www\teach\teach\upload\\'; //设置文件上传路径,服务器上的绝对路径
$id = $_GET['id']; //GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。
if($id == "") //如果id为空而出错时,程序跳转到项目的Index/index页面。或可做其他处理。
{
echo '<script language="javascript">alert("没有此文件");</script>';
}
$file = D("fileView"); //利用与表file对应的数据模型类FileModel来建立数据对象。
$map = array( 'file.id' => $id );
$result = $file->where($map)->select(); //根据id查询到文件信息
if($result==false) //如果查询不到文件信息而出错时,程序跳转到项目的Index/index页面,或可做其他处理
{
$this->redirect('index','Index',"APP_NAME",1);
}else {
$savename = $result[0]['newname']; //文件保存名
$showname = $result[0]['oldname']; //文件原名
$filename = $uploadPath.$savename; //完整文件名(路径加名字)
$tfile = $showname;
$tfile = urldecode($tfile);
// ../ is not allowed in the file name
if (!ereg("(\.\.\/)", $tfile))
{
// Does the file exist?
if (file_exists($uploadPath. $savename))
{
$fp = @fopen( $uploadPath.$savename, "r" );
// Prompt the user to download the new torrent file.
Header( "Expires: 0" );
Header( "Pragma: public");
Header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
Header( "Cache-Control: public");
header("Content-Type:application/download");;
Header( "Content-Type: application/octet-stream");
if (strstr($_SERVER["HTTP_USER_AGENT"],"MSIE"))
{
Header( "Content-Disposition: attachment; filename=" . urlencode($tfile) );
}
else
{
Header( "Content-Disposition: attachment; filename=" . $tfile );
}
Header( "Content-transfer-encoding: binary");
Header( "Content-length: " . @filesize( $uploadPath . $savename ));
// Send the torrent file
@fpassthru( $fp );
@fclose( $fp );
}
}
}
echo "1111111111111111111111111111111111111111";
//exit();
}
public function download3(){
$uploadPath = 'C:\AppServ\www\teach\teach\upload\\'; //设置文件上传路径,服务器上的绝对路径
$id = $_GET['id']; //GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。
if($id == "") //如果id为空而出错时,程序跳转到项目的Index/index页面。或可做其他处理。
{
echo '<script language="javascript">alert("没有此文件");</script>';
}
$file = D("fileView"); //利用与表file对应的数据模型类FileModel来建立数据对象。
$map = array( 'file.id' => $id );
$result = $file->where($map)->select(); //根据id查询到文件信息
if($result==false) //如果查询不到文件信息而出错时,程序跳转到项目的Index/index页面,或可做其他处理
{
$this->redirect('index','Index',"APP_NAME",1);
}else {
$savename = $result[0]['newname']; //文件保存名
$showname = $result[0]['oldname']; //文件原名
$filename = $uploadPath.$savename; //完整文件名(路径加名字)
$durl = $filename;
$filename = $showname;
$file = @fopen($durl, 'r');
header("Content-Type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($durl));
header("Content-Disposition: attachment; filename=".$filename);
echo fread($file,filesize($durl));
fclose($file);
}
$this->redirect('look');
}
public function download( ){
import("ORG.Net.Http");
// $uploadPath = 'C:\AppServ\www\teach\teach\upload\\'; //设置文件上传路径,服务器上的绝对路径
// $id = $_GET['id']; //GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。
// if($id == "") //如果id为空而出错时,程序跳转到项目的Index/index页面。或可做其他处理。
// {
// echo '<script language="javascript">alert("没有此文件");</script>';
// }
//
// $file = D("fileView"); //利用与表file对应的数据模型类FileModel来建立数据对象。
// $map = array( 'file.id' => $id );
// $result = $file->where($map)->select(); //根据id查询到文件信息
//
// if($result==false) //如果查询不到文件信息而出错时,程序跳转到项目的Index/index页面,或可做其他处理
// {
// $this->redirect('index','Index',"APP_NAME",1);
// }else {
// $savename = $result[0]['newname']; //文件保存名
// $showname = $result[0]['oldname']; //文件原名
// $filename = $uploadPath.$savename; //完整文件名(路径加名字)
//
// //echo $filename." ".$savename;
Http::download(__FILE__,$showname);
//}
//$this->display();
}
static function download2 ($filename, $showname="",$content="",$expire=180) {
import("ORG.Net.Http");
//$uploadPath = 'http://'.$_SERVER['HTTP_HOST'].'/teach/teach/upload/';
$uploadPath = 'C:\AppServ\www\teach\teach\upload\\';
//$uploadPath = "\\SERVER-PC\\www\\teach\\teach\\upload\\"; ’H:\www\web\uploads\’; //设置文件上传路径,服务器上的绝对路径
$id = $_GET['id']; //GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。
if($id == "") //如果id为空而出错时,程序跳转到项目的Index/index页面。或可做其他处理。
{
echo '<script language="javascript">alert("没有此文件");</script>';
}
$file = D("fileView"); //利用与表file对应的数据模型类FileModel来建立数据对象。
$map = array( 'file.id' => $id );
$result = $file->where($map)->select(); //根据id查询到文件信息
if($result==false) //如果查询不到文件信息而出错时,程序跳转到项目的Index/index页面,或可做其他处理
{
$this->redirect('index','Index',"APP_NAME",1);
}else {
$savename = $result[0]['newname']; //文件保存名
$showname = $result[0]['oldname']; //文件原名
$filename = $uploadPath.$savename; //完整文件名(路径加名字)
}
if(file_exists($filename))
{
$length = filesize($filename);
}
else if(is_file($uploadPath.$filename))
{
$filename = $uploadPath.$filename;
$length = filesize($filename);
}
else if($content != "") {
$length = strlen($content);
}
else {
throw_exception($filename.L('_DOWN_FILE_NOT_EXIST_'));
}
if(empty($showname))
{
$showname = $filename;
}
$showname = basename($showname);
if(empty($filename))
{
$type = mime_content_type($filename);
}
else
{
$type = "application/octet-stream";
}
//发送Http Header信息 开始下载
header("content-type:text/html; charset=utf-8");
header("Pragma: public");
header("Cache-control: max-age=".$expire);
//header(‘Cache-Control: no-store, no-cache, must-revalidate’);
header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
//下面一行就是改动的地方
//即用iconv("UTF-8″,"GB2312//TRANSLIT",$showname)系统函数转换编码为gb2312
header("Content-Disposition: attachment; filename=".iconv('UTF-8','GB2312//TRANSLIT',$showname));
header("Content-Length: ".$length);
header("Content-type: ".$type);
header('Content-Encoding: none');
header("Content-Transfer-Encoding: binary" );
if($content == "") {
readfile($filename);
}else {
echo($content);
}
echo "songshichao";
//$this->display();
}
//
// 注:iconv为php系统函数库,但需要安装。若是服务器还没有这个模块安装,则需将iconv.dll下载下来后复制到windows/system32/下面,同时在php安装文件夹得ext文件夹里也复制一份。
// 然后在php.ini文件中将extension=php_iconv.dll前的";"去掉,没有的话就加上extension=php_iconv.dll。然后重启服务器即可。
- ThinkPhp文件下载问题
- thinkphp 的文件下载
- thinkphp 的文件下载
- thinkphp实现文件下载
- thinkphp 文件下载
- thinkphp 文件下载 文件下载php
- thinkphp文件上传与下载
- thinkphp实现文件下载函数
- thinkPHP上传文件问题
- thinkphp使用http::download下载word或者ppt文件乱码问题的解决方法
- Thinkphp中的文件路径问题
- [php] thinkphp基于Http类 下载文件
- thinkphp控制器实现隐藏文件路径下载
- Thinkphp ajax异步下载excel文件
- ThinkPHP模板文件中的路径问题
- thinkPHP中Model文件加下类的问题
- ThinkPHP 解决模板文件大小写问题
- thinkphp实现当前页面点击下载文件实例
- u-boot中.lds连接脚本文件的分析
- Tekkaman Ninja 版本的uboot说明
- poj-2586-Y2K Accounting Bug
- 怎样为Eclipse设置字符集
- 详解公钥、私钥、数字证书的概念
- ThinkPhp文件下载问题
- http 协议详解
- 资料样板的定义与套用
- RedHat5.5+IBM DS4800+Oracle10g RAC+LVM安装
- WMI012-WMI学习笔记(十二)——Win32_ApplicationService
- 小波变换网文精粹:小波变换和motion信号处理(六)
- 静态初始化块
- Java 图形用户界面-向JApplet添加组件
- oracle 日期函数的使用