怎样才能用PHP语言实现网页中的下载文件功能

PHP如何编写文件下载的代码,麻烦注明一下文件路径和文件名的填写,谢谢!

<?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;
        }        
?>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-14
$user_file='要下载的文件路径';
header("Content-Type: application/force-download"); 
header("Content-Disposition: attachment; filename=".basename($user_file)); 
readfile($user_file);

追问

为何下载下来的都是SDF文件?

追答

你好,我的代码是没有问题的.
请检查你的PHP运行环境是否还存在其他问题.

本回答被提问者采纳