PHP遍历文件夹内容

比眉伴天荒 2022-08-04 04:17 383阅读 0赞
  1. 遍历文件,没什么技术含量,看了手册,还有事例代码,稍微修改一下,把文件夹下面的文件夹继续变量就OK了。
  2. function listDir($dir){
  3. //判断是否是文件夹
  4. if(is_dir($dir)){
  5. // 打开文件夹
  6. if ($dh = opendir($dir)) {
  7. //读取文件夹
  8. while (($file = readdir($dh)) !== false) {
  9. //如果文件夹中还有文件夹就继续遍历
  10. //把 .和..排除
  11. if(is_dir($dir.'/'.$file) && $file != '.' && $file != '..'){
  12. echo '<hr>'."文件夹: $file ". "<br>";
  13. //继续遍历
  14. listDir($dir."/".$file."/");
  15. }else{
  16. //输出文件夹里面的内容
  17. if($file != '.' && $file != '..')
  18. echo $file.'<br>';
  19. }
  20. }
  21. closedir($dh);
  22. }
  23. }
  24. }
  25. listDir("D:\soft");

Center

删除一个文件夹目录

  1. function deldir($dir) {
  2. $dh=opendir($dir);
  3. while ($file=readdir($dh)) {
  4. if($file!="." && $file!="..") {
  5. $fullpath=$dir."/".$file;
  6. if(!is_dir($fullpath)) {
  7. unlink($fullpath);
  8. } else {
  9. deldir($fullpath);
  10. }
  11. }
  12. }
  13. closedir($dh);
  14. if(rmdir($dir)) {
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }

发表评论

表情:
评论列表 (有 0 条评论,383人围观)

还没有评论,来说两句吧...

相关阅读