求PHP-readfile下载中的问题 本帖最后由 kejzhao 于 2010-07-19 16:14:34 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你使用 readfile 要注意看看文件前后是否有空格 除了使用readfile来下载文件外,还有其他方式来下载文件吗? 下载文件前后没有空格要下载一个tar包,应该不存在前后空格的问题有环境的可以试一下,上面的程序可以直接调用的。 <?php/* * To change this template, choose Tools | Templates * and open the template in the editor. */download(dirname(__FILE__).'/Desktop.rar');function download($sFilePath){ //解析文件路径,分离文件名 $aFilePath = explode("/",str_replace("\\","/",$sFilePath)); $sFileName = $aFilePath[count($aFilePath) - 1]; //获取文件大小 @$nFileSize = filesize($sFilePath); //设置文件头 header("Pramga:public"); header("Expires:0"); header("Cache-Control:private"); header("Cache-Component:must-revalidate,post-check=0,pre-check=0"); header("Content-Disposition:attachment;filename=".$sFileName); header("Content-Length:".$nFileSize); @header("Content-type:".mime_content_type($sFilePath)); header("Content-Encoding:none"); header("Content-Transfer-Encoding:binary"); //下载 if(!connection_aborted() && readfile($sFilePath) != false) return 0; else return 1;}我下来测试了一下,可以实现啊,没有空格,rar也能正常打开。?> 搞不懂要那么多header函数干什么,这里有个例子 http://www.vshu.com/archives/257例子中用了三个header函数,其实,第三个header的作用是保证IE在https协议下也能正常下载,如果在http协议下,两个header就够了。还有,例子中的文件是动态生成的,如果是实际存在的文件,把文件读进变量再输出就OK了。 补充一点,Content-type 指的是文件的MIME类型,比如文本文件要用 text/plain ,二进制文件要用 application/octet-stream,不能搞错 DZ 这一段代码的用意是什么啊 一个页面转向的问题 跨服务器查询数据库出现编码问题 新手系列之六:请大家帮我看下这个if语句哪里出了问题??? download 时产生多余信息 用ora如何接收oracle存储过程的多行返回(返回数据集) mysql_fetch_row问题 [调查]多少人在用xoops?回复有分 php能够读取flash文件的长和宽吗? 问下怎么生成所有的不重复的数组 请问javascript文件可以这样使用吗?简单的问题求助~~ 139.com无法接收邮件。在线跪求
看看文件前后是否有空格
下载文件前后没有空格
要下载一个tar包,应该不存在前后空格的问题有环境的可以试一下,上面的程序可以直接调用的。
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/download(dirname(__FILE__).'/Desktop.rar');function download($sFilePath){
//解析文件路径,分离文件名
$aFilePath = explode("/",str_replace("\\","/",$sFilePath));
$sFileName = $aFilePath[count($aFilePath) - 1];
//获取文件大小
@$nFileSize = filesize($sFilePath);
//设置文件头
header("Pramga:public");
header("Expires:0");
header("Cache-Control:private");
header("Cache-Component:must-revalidate,post-check=0,pre-check=0");
header("Content-Disposition:attachment;filename=".$sFileName);
header("Content-Length:".$nFileSize);
@header("Content-type:".mime_content_type($sFilePath));
header("Content-Encoding:none");
header("Content-Transfer-Encoding:binary");
//下载
if(!connection_aborted() && readfile($sFilePath) != false)
return 0;
else
return 1;
}
我下来测试了一下,可以实现啊,没有空格,rar也能正常打开。
?>