php、asp以“流格式”下载服务器端文件

解决WORD,EXCEL,TXT,图片等文件类型直接在IE中被打开的问题

ASP/Visual Basic代码
  1. <%   
  2.     Function downFileFromServer(showFileName, downFilePath)   
  3.         response.Clear   
  4.         response.Buffer = true   
  5.         Set ads = Server.CreateObject("ADODB.Stream")   
  6.         ads.Type = 1   
  7.         ads.Mode = 3   
  8.         ads.Open   
  9.         ads.LoadFromFile downFilePath   
  10.         response.AddHeader("Content-Encoding""None")   
  11.         response.AddHeader("Cache-Control""Private")   
  12.         response.AddHeader("Content-Length", ads.Size)   
  13.         response.AddHeader("Content-Disposition""attachment; filename=" & showFileName)   
  14.         response.ContentType = "application/octet-stream"  
  15.         response.BinaryWrite(ads.Read(ads.Size))   
  16.         ads.Close   
  17.         Set ads = nothing   
  18.         response.Flush   
  19.         response.End  
  20.     End Function  
  21.   
  22.     Call downFileFromServer("abc.test", Server.MapPath("test.asp"))   
  23. %>   
  24.   

 

PHP代码
  1. <?php   
  2.     function downFileFromServer($showFileName$downFilePath)   
  3.     {   
  4.         if(file_exists($downFilePath))   
  5.         {   
  6.             if(is_readable($downFilePath))   
  7.             {   
  8.                 if(Trim($showFileName) == '')   
  9.                 {   
  10.                 $showFileName = 'undefined';   
  11.                 }   
  12.                 ob_start();   
  13.                 ob_clean();   
  14.                 $file_size = filesize($downFilePath);   
  15.                 header('Content-Encoding:none');   
  16.                 header('Cache-Control:private');   
  17.                 header('Content-Length:' . $file_size);   
  18.                 header('Content-Disposition:attachment; filename=' . $showFileName);   
  19.                 header('Content-Type:application/octet-stream');   
  20.                 readfile($downFilePath);   
  21.                 ob_flush();   
  22.             }   
  23.         }   
  24.     }   
  25.   
  26.     //Sample    
  27.     downFileFromServer('a.abc''test.php');   
  28. ?>   

 



[本日志由 admin 于 2012-04-13 01:28 PM 更新]
上一篇: php遍历文件夹和文件
下一篇: php javascript cookie中文乱码解决办法
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: asp php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 1665
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑