您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从服务器php下载文件

从服务器php下载文件

要读取目录内容,可以使用readdir()并使用脚本(在我的示例中download.PHP)来下载文件

if ($handle = opendir('/path/to/your/dir/')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo "<a href='download.PHP?file=".$entry."'>".$entry."</a>\n";
        }
    }
    closedir($handle);
}

在其中,download.PHP您可以强制浏览器发送下载数据,并使用basename()来确保客户端不会传递其他文件名,例如../config.PHP

$file = basename($_GET['file']);
$file = '/path/to/your/dir/'.$file;

if(!file_exists($file)){ // file does not exist
    die('file not found');
} else {
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/zip");
    header("Content-@R_746_301@: binary");

    // read the file from disk
    readfile($file);
}
php 2022/1/1 18:22:24 有424人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶