求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,不能搞错 今儿被lvs害苦了 在哪运行PHP代码? php程序中应用swfupload上传关于SESSION的问题 关于php解析xml的问题。 linux中,如何用php把一个域名绑定到某一个目录? 关于 变量的赋值 急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急急 怎样用PHP实现游标的功能(我用的是MYSQL的数据库) 如何在PHP中取得服务器的IP地址? Help!---如何在Oracle中保存一幅图片? 请问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也能正常打开。
?>