使用PHP函數(shù)scandir排除特定目錄
更新時(shí)間:2014年06月12日 14:28:01 投稿:shichen2014
scandir()函數(shù)返回一個(gè)數(shù)組,其中包含指定路徑中的文件和目錄。這篇文章主要介紹了使用PHP函數(shù)scandir排除特定目錄,需要的朋友可以參考下
scandir()函數(shù)返回一個(gè)數(shù)組,其中包含指定路徑中的文件和目錄。如下所示:
例子:
復(fù)制代碼 代碼如下:
<?php
print_r(scandir('test_directory'));
?>
print_r(scandir('test_directory'));
?>
輸出:
復(fù)制代碼 代碼如下:
Array
(
[0]=>.
[1]=>..
[2]=>1.txt
[3]=>2.txt
)
(
[0]=>.
[1]=>..
[2]=>1.txt
[3]=>2.txt
)
大部分情況只需要該目錄的文件列表數(shù)組,如下:
復(fù)制代碼 代碼如下:
Array
(
[0]=>1.txt
[1]=>2.txt
)
(
[0]=>1.txt
[1]=>2.txt
)
一般是通過(guò)排除“.”或者“..”的數(shù)組項(xiàng)解決的:
復(fù)制代碼 代碼如下:
<?php
functionfind_all_files($dir)
{
$root = scandir($dir);
foreach($rootas$value)
{
if($value === '.' || $value === '..'){
continue;
}
if(is_file("$dir/$value")){
$result[] = "$dir/$value";
continue;
}
foreach(find_all_files("$dir/$value")as$value)
{
$result[] = $value;
}
}
return$result;
}
?>
functionfind_all_files($dir)
{
$root = scandir($dir);
foreach($rootas$value)
{
if($value === '.' || $value === '..'){
continue;
}
if(is_file("$dir/$value")){
$result[] = "$dir/$value";
continue;
}
foreach(find_all_files("$dir/$value")as$value)
{
$result[] = $value;
}
}
return$result;
}
?>
另外一種方法,利用array_diff函數(shù),剔除scandir函數(shù)執(zhí)行得到的數(shù)組:
復(fù)制代碼 代碼如下:
<?php
$directory='/path/to/my/directory';
$scanned_directory=array_diff(scandir($directory),array('..','.'));
?>
$directory='/path/to/my/directory';
$scanned_directory=array_diff(scandir($directory),array('..','.'));
?>
通常情況代碼管理會(huì)產(chǎn)生.svn文件,或者限制目錄訪問(wèn)權(quán)限的.htaccess等文件。所以通過(guò)array_diff函數(shù)來(lái)過(guò)濾會(huì)更方便。
您可能感興趣的文章:
- php中目錄操作opendir()、readdir()及scandir()用法示例
- PHP獲取當(dāng)前文件所在目錄 getcwd()函數(shù)
- php文件夾與文件目錄操作函數(shù)介紹
- php中判斷文件空目錄是否有讀寫權(quán)限的函數(shù)代碼
- PHP解析目錄路徑的3個(gè)函數(shù)總結(jié)
- PHP使用glob函數(shù)遍歷目錄或文件夾的方法
- php file_exists 檢查文件或目錄是否存在的函數(shù)
- PHP遍歷目錄函數(shù)opendir()、readdir()、closedir()、rewinddir()總結(jié)
- PHP刪除非空目錄的函數(shù)代碼小結(jié)
- PHP目錄函數(shù)實(shí)現(xiàn)創(chuàng)建、讀取目錄教程實(shí)例
- php使用scandir()函數(shù)掃描指定目錄下所有文件示例
相關(guān)文章
PHP生成不同顏色、不同大小的tag標(biāo)簽函數(shù)
看別人網(wǎng)站上面的tag都是不同顏色,不同大小的tag標(biāo)簽非常不錯(cuò),這里就分享兩個(gè)函數(shù),方便需要的朋友2013-09-09php實(shí)現(xiàn)的簡(jiǎn)單美國(guó)商品稅計(jì)算函數(shù)
這篇文章主要介紹了php實(shí)現(xiàn)的簡(jiǎn)單美國(guó)商品稅計(jì)算函數(shù),涉及php數(shù)學(xué)計(jì)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07php動(dòng)態(tài)函數(shù)調(diào)用方法
本文主要給大家介紹了php中動(dòng)態(tài)調(diào)用函數(shù)的方法,實(shí)例分析了php動(dòng)態(tài)函數(shù)的實(shí)現(xiàn)原理與具體實(shí)現(xiàn)步驟,需要的朋友可以參考下2015-05-05