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 $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菜刀如何下载文件夹的详细内容,更多请关注叮当号网其它相关文章!
文章来自互联网,只做分享使用。发布者:城南北边,转转请注明出处:https://www.dingdanghao.com/article/689584.html