PHP 菜刀下载文件夹步骤:1. 获取文件夹内文件列表;2. 循环文件,判断是否为目录,若不是则获取文件大小、创建下载头、读取文件内容并输出。
如何使用 PHP 菜刀下载文件夹
步骤 1:获取文件列表
$dir = '/path/to/directory'; $files = scandir($dir);
步骤 2:循环文件并下载
foreach($files as $file) { if (is_dir($dir . '/' . $file)) { continue; // 跳过目录 } // 获取文件大小 $size = filesize($dir . '/' . $file); // 创建下载头 header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"" . basename($file) . "\""); header("Content-Length: " . $size); // 读取文件内容并输出 readfile($dir . '/' . $file); }
示例代码:
立即学习“PHP免费学习笔记(深入)”;
<?php $dir = '/path/to/directory'; $files = scandir($dir); foreach($files as $file) { if (is_dir($dir . '/' . $file)) { continue; } $size = filesize($dir . '/' . $file); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"" . basename($file) . "\""); header("Content-Length: " . $size); readfile($dir . '/' . $file); } ?>
以上就是php菜刀如何下载文件夹的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号