怎样才能用PHP语言实现网页中的下载文件功能
发布网友
发布时间:2022-04-25 21:15
我来回答
共2个回答
热心网友
时间:2022-04-28 07:42
<?php
/*
$file------文件名
$_SERVER['DOCUMENT_ROOT']-----服务器跟目标
down------自定义下载文件的文件夹
获取文件在文件夹里面的位置
必须是绝对路径
Content-Type: application/force-download 强制浏览器下载
*/
$file="xxxx.rar";
$file=$_SERVER['DOCUMENT_ROOT']."/down/".$file ;
if(is_file($file)) {
header("Content-Type: application/force-download");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
exit;
}else{
echo "文件不存在!";
exit;
}
?>
热心网友
时间:2022-04-28 09:00
$user_file='要下载的文件路径';
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($user_file));
readfile($user_file);
追问
为何下载下来的都是SDF文件?
追答你好,我的代码是没有问题的.
请检查你的PHP运行环境是否还存在其他问题.