[英]How to properly show all files in directory and sub directory via php
我有4個文件夾,我想檢查其中的圖像和php文件。 這是結構文件夾的布置。
main_folder包含兩個子文件夾(second_folder和third_folder),而third_folder包含一個子文件夾(fourth_folder) ,依此類推。
當前使用下面的代碼,我只能訪問first_main_folder中的所有文件。
$files = glob('first_Main_folder/*.{jpg,png,gif,php}', GLOB_BRACE);
foreach($files as $file) {
echo "<br>$file<br>";
}
請如何訪問其余子文件夾(second_folder,third_folder,fourth_folder)等中的文件
謝謝
first_Main_folder
test.png
data.php
check.gif
second_folder
tony.jpg
mark.gif
action.php
third_folder
index.php
post.php
sender.php
han.gif
fourth_folder
catch.php
index2.php
redirect.php
and so on
下面的此功能將掃描主目錄和所有子目錄。 這個對我有用。 謝謝。
function outputFiles1($path){
// Check directory exists or not
if(file_exists($path) && is_dir($path)){
// Search the files in this directory
$files = glob($path ."/*");
if(count($files) > 0){
// Loop through retuned array
foreach($files as $file){
if(is_file("$file")){
// Display only filename
echo basename($file) . filesize($file) . "<br>";
} else if(is_dir("$file")){
// Recursively call the function if directories found
outputFiles1("$file");
}
}
} else{
echo "ERROR: No such file found in the directory.";
}
} else {
echo "ERROR: The directory does not exist.";
}
}
// Call the function
outputFiles1("C:/xampp/htdocs/first_main_folder");
以下示例摘自本文 。
function rsearch($folder, $pattern='/.*/') {
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
$fileList = array();
foreach($files as $file) {
$fileList = array_merge($fileList, $file);
}
return $fileList;
}
$result = rsearch('./');
print_r($result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.