现在的位置: 首页 > 综合 > 正文

ThinkPhp文件下载问题

2014年06月05日 ⁄ 综合 ⁄ 共 8622字 ⁄ 字号 评论关闭

ThinkPhp文件下载问题
2011-03-30 18:04

作者:宋世超 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。然后重启服务器即可。

抱歉!评论已关闭.