日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

php遍历文件夹下文件内容_php遍历文件夹下所有文件的代码示例

發(fā)布時間:2024/10/8 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php遍历文件夹下文件内容_php遍历文件夹下所有文件的代码示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本篇文章給大家?guī)淼膬?nèi)容是關于php遍歷文件夾下所有文件的代碼示例,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

不論是面試還是正常工作需要都會用到遍歷文件夾下的所有文件,今天就記錄一下筆記。廢話不多說直接上代碼:<?php

/**

* 遍歷當前文件夾展示所有的文件和目錄

*/

function dirList($dir_path = '') {

if(is_dir($dir_path)) {

$dirs = opendir($dir_path);

if($dirs) {

while(($file = readdir($dirs)) !== false) {

if($file !== '.' && $file !== '..') {

if(is_dir($file)) {

echo $dir_path . '/' . $file . '
';

dirList($dir_path . '/' . $file);

} else {

echo $dir_path . '/' . $file . '
';

}

}

}

closedir($dirs);

}

} else {

echo '目錄不存在!';

}

}

dirList('/var/www/html/php-demo');

function dir_list($dir) {

if(!is_dir($dir)) return false;

$dir_list = array();

$opendir = opendir($dir);

if($opendir) {

while(($file = readdir($opendir)) !== false) {

if($file !== '.' && $file !== '..') {

$tem = $dir . '/' . $file;

if(is_dir($tem)) {

$dir_list[$tem . '/'] = $file . '/';

dir_list($tem);

} else {

$dir_list[] = $file;

}

}

}

closedir($opendir);

return $dir_list;

}

}

$dir = dir_list('/var/www/html/php-demo');

var_dump($dir);

運行結(jié)果:

源代碼已上傳GitHub:https://github.com/cuiyuanxin/php-demo/blob/master/dir.php

總結(jié)

以上是生活随笔為你收集整理的php遍历文件夹下文件内容_php遍历文件夹下所有文件的代码示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。