欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

pHP文件下载代码,求各位大神给看看那哪里错了

程序员文章站 2022-04-16 08:54:44
...
//文件下载函数
//$file_path 文件下载路径
//$file_name 文件名
//$file_sub_dir 下载文件子路径
function down_file($file_name,$file_sub_dir){
//如果文件名字是中文,转码
$file_name=iconv("utf-8", "gb-2312", $file_name);
//绝对路径
$file_path=$_SERVER["DOCUMENT_ROOT"].$file_sub_dir.$file_name;
//打开文件
if(!file_exists($file_path)){
echo "文件不存在!";
return;
}
$fp=fopen($file_path, "r");

//获取文件大小
$file_size=filesize($file_path);

//返回的文件
header("Content-type: application/octet-stream");
//按照字节大小返回
header("Accept-Ranges: bytes");
//返回文件大小
header("Accept-length: $file_size");
//客户端弹出对话框,对应的文件名
header("Content-Disposition: attachment; file_name=".$file_name);

//向客户端回送数据

$buffer=1024;
//为了下载安全,做个文件字节读取技术器
$file_count=0;
//判断文件是否结束
while (!feof($fp) && ($file_size-$file_count>0)){
$file_data=fread($fp, $buffer);
//读取了多少字节
$file_count+=$buffer;
//把部分数据传给浏览器
echo $file_data;

}
//关闭文件
fclose($fp);
}
file_down("1.jpg","/test/");
?>


回复讨论(解决方案)

下面是错误

Warning: fopen(C:/AppServ/www/test/) [function.fopen]: failed to open stream: Permission denied in C:\AppServ\www\test\FileDownServer.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 22

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 26

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\test\FileDownServer.php:16) in C:\AppServ\www\test\FileDownServer.php on line 28

Warning: feof(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 36

Warning: fread(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 37

Warning: feof(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 36

Warning: fread(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 37

Warning: feof(): supplied argument is not a valid stream resource in C:\AppServ\www\test\FileDownServer.php on line 36

Warning: fopen(C:/AppServ/www/test/) [function.fopen]: failed to open stream: Permission denied in C:\AppServ\www\test\FileDownServer.php on line 16
说的很明确了!
Permission denied 中文释义:权限被拒绝