php网站 if $fd,PHP: fpassthru - Manual

I have also perused this list of examples which I am sure work for that person, but, as others have mentioned here, do not work for me or (anyone else).

So what I did was try out all of these examples, check other sources of information, and put together what I think to be an example of what works on 'more than a few' systems.  The following example works for me wherever I need to create a download using fpassthru(), which works with IE6 (among other browsers):

/*/

Download a file using fpassthru()

/*/

$fileDir = "/home/pathto/myfiles"; // supply a path name.

$fileName = "myfile.zip"; // supply a file name.

$fileString=$fileDir.'/'.$fileName; // combine the path and file

// translate file name properly for Internet Explorer.

if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")){

$fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1);

}

// make sure the file exists before sending headers

if(!$fdl=@fopen($fileString,'r')){

die("Cannot Open File!");

} else {

header("Cache-Control: ");// leave blank to avoid IE errors

header("Pragma: ");// leave blank to avoid IE errors

header("Content-type: application/octet-stream");

header("Content-Disposition: attachment; filename=\"".$fileName."\"");

header("Content-length:".(string)(filesize($fileString)));

sleep(1);

fpassthru($fdl);

}

?>

All that should require editing is the $fileDir and $fileName variables.  Upload the file and point to it with your browser to see if the script will prompt you for a download.

NOTE : Regarding File Types : Leaving the 'Content-type' header as-is should allow you to download pretty much any file.  I have tested it on some of the more popular file types including zip, css, php, inc, htm, png, gif and jpg.  During these tests, I did note that if I selected 'cancel' or 'open' when prompted to download either a gif or jpg, that it would indeed cancel or open in my image browser as it should, but subsequent attempts at 'downloading only' yielded a web page view of the image.  Closing the window and opening a new one reset this, allowing me save a jpeg or gif to the hard drive directly.  I believe the problem lies in the way the caching headers are treated, since if any info is specified in the 'cache-control' header, the browser download fails completely (in IE, anyways).

Enjoy! Mail me if it works!  ;-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值