php遍歷文件夾和文件列表示例分享
為PHP遍歷目錄和文件列表寫了一個(gè)簡(jiǎn)單的類,并附上使用實(shí)例,大家參考使用吧
<?php
define('DS', DIRECTORY_SEPARATOR);
class getDirFile{
//返回?cái)?shù)組
private $DirArray = array();
private $FileArray = array();
private $DirFileArray = array();
private $Handle,$Dir,$File;
//獲取目錄列表
public function getDir( & $Dir ){
if( is_dir($Dir) ){
if( false != ($Handle = opendir($Dir)) ){
while( false != ($File = readdir($Handle)) ){
if( $File!='.' && $File!='..' && !strpos($File,'.') ){
$DirArray[] = $File;
}
}
closedir( $Handle );
}
}else{
$DirArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
}
return $DirArray;
}
//獲取文件列表
public function getFile( & $Dir ){
if( is_dir($Dir) ){
if( false != ($Handle = opendir($Dir)) ) {
while( false != ($File = readdir($Handle)) ){
if( $File!='.' && $File!='..' && strpos($File,'.') ){
$FileArray[] = $File;
}
}
closedir( $Handle );
}
}else{
$FileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
}
return $FileArray;
}
//獲取目錄/文件列表
public function getDirFile( & $Dir ){
if( is_dir($Dir) ){
$DirFileArray['DirList'] = $this->getDir( $Dir );
if( $DirFileArray ){
foreach( $DirFileArray['DirList'] as $Handle ){
$File = $Dir.DS.$Handle;
$DirFileArray['FileList'][$Handle] = $this->getFile( $File );
}
}
}else{
$DirFileArray[] = '[Path]:\''.$Dir.'\' is not a dir or not found!';
}
return $DirFileArray;
}
}
?>
實(shí)例:(相對(duì)路徑或絕對(duì)路徑)
1.獲取目錄列表
<?php
$Dir_dir = './example';
$getDirFile = new getDirFile();
$getDir = $getDirFile->getDir( $Dir_dir );
print_r($getDir);
?>
顯示
<?php
$File_one_dir = './example/example_one';
$File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';
$getDirFile = new getDirFile();
$getFile_one = $getDirFile->getFile( $File_one_dir );
$getFile_two = $getDirFile->getFile( $File_two_dir );
print_r($getFile_one);
print_r($getFile_two);
?>
2.獲取文件列表
<?php
$File_one_dir = './example/example_one';
$File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';
$getDirFile = new getDirFile();
$getFile_one = $getDirFile->getFile( $File_one_dir );
$getFile_two = $getDirFile->getFile( $File_two_dir );
print_r($getFile_one);
print_r($getFile_two);
?>
顯示
Array
(
[0] => example.sql
[1] => example.txt
)
Array
(
[0] => example.php
)
3.獲取目錄/文件列表
<?php
$Dir_dir = './example';
$getDirFile = new getDirFile();
$getDirFile = $getDirFile->getDirFile( $Dir_dir );
print_r($getDirFile);
?>
顯示
Array
(
[DirList] => Array
(
[0] => example_one
[1] => example_two
)
[FileList] => Array
(
[example_one] => Array
(
[0] => example.sql
[1] => example.txt
)
[example_two] => Array
(
[0] => example.php
)
)
)
相關(guān)文章
Laravel (Lumen) 解決JWT-Auth刷新token的問題
今天小編就為大家分享一篇Laravel (Lumen) 解決JWT-Auth刷新token的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php使用自帶dom擴(kuò)展進(jìn)行元素匹配的原理解析
這篇文章主要介紹了php使用自帶dom擴(kuò)展進(jìn)行元素匹配的原理解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
thinkphp視圖模型查詢提示ERR: 1146:Table ''db.pr_order_view'' doesn''
這篇文章主要介紹了thinkphp視圖模型查詢提示ERR: 1146:Table 'db.pr_order_view' doesn't exist的解決方法,對(duì)于ThinkPHP初學(xué)者來說有一定的借鑒價(jià)值,需要的朋友可以參考下2014-10-10
在 Laravel 項(xiàng)目中使用 webpack-encore的方法
這篇文章主要介紹了在 Laravel 項(xiàng)目中使用 webpack-encore的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
Laravel5.7框架安裝與使用學(xué)習(xí)筆記圖文詳解
這篇文章主要介紹了Laravel5.7框架安裝與使用學(xué)習(xí)筆記,結(jié)合圖文形式詳細(xì)講解了Laravel5.7框架的安裝、配置、組件、路由等基礎(chǔ)與操作技巧,需要的朋友可以參考下2019-04-04
php調(diào)用dll的實(shí)例操作動(dòng)畫與代碼分享
這是我錄制的一個(gè)gif操作動(dòng)畫,圖片比較大,如果大家在線看圖感覺不流暢的話可以把圖片保存到本機(jī)再看2012-08-08

