php循环读取文件夹里的文件,PHP读取子目录并遍历文件如何?

PHP读取子目录并遍历文件如何?

我需要创建一个遍历子目录中所有文件的循环。 您能帮我构造我的代码吗?

$main = "MainDirectory";

loop through sub-directories {

loop through filels in each sub-directory {

do something with each file

}

};

M.E asked 2020-06-26T05:30:12Z

9个解决方案

141 votes

将RecursiveDirectoryIterator与RecursiveIteratorIterator结合使用。

$di = new RecursiveDirectoryIterator('path/to/directory');

foreach (new RecursiveIteratorIterator($di) as $filename => $file) {

echo $filename . ' - ' . $file->getSize() . ' bytes
';

}

Michał Rudnicki answered 2020-06-26T05:30:21Z

10 votes

如果您的子目录包含子子目录,则可能需要为此使用递归函数

$main = "MainDirectory";

function readDirs($main){

$dirHandle = opendir($main);

while($file = readdir($dirHandle)){

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

readDirs($file);

}

else{

//do stuff

}

}

}

没有测试代码,但这应该接近您想要的。

GSto answered 2020-06-26T05:30:46Z

7 votes

您需要将路径添加到递归调用中。

function readDirs($path){

$dirHandle = opendir($path);

while($item = readdir($dirHandle)) {

$newPath = $path."/".$item;

if(is_dir($newPath) && $item != '.' && $item != '..') {

echo "Found Folder $newPath
";

readDirs($newPath);

}

else{

echo '  Found File or .-dir '.$item.'
';

}

}

}

$path = "/";

echo "$path
";

readDirs($path);

John Marty answered 2020-06-26T05:31:06Z

5 votes

我喜欢RecursiveDirectoryIterator和它的通配符:

foreach (glob("*/*.txt") as $filename) {

echo "$filename\n";

}

详细信息和更复杂的方案。

但是,如果您具有复杂的文件夹结构,则绝对可以使用RecursiveDirectoryIterator。

Fedir RYKHTIK answered 2020-06-26T05:31:35Z

4 votes

来吧,先自己尝试!

您需要什么:

scandir()

is_dir()

当然是foreach

[HTTP://PHP.net/manual/恩/function.is-敌人.PHP]

[HTTP://PHP.net/manual/恩/function.三餐敌人.PHP]

psaniko answered 2020-06-26T05:32:12Z

2 votes

读取子目录和子文件(设置正确的文件夹名称)的另一种解决方案:

$path = realpath('samplefolder/yorfolder');

foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)

{

echo "$filename
";

}

?>

T.Todua answered 2020-06-26T05:32:32Z

1 votes

ini_set('max_execution_time', 300); // increase the execution time of the file (in case the number of files or file size is more).

class renameNewFile {

static function copyToNewFolder() { // copies the file from one location to another.

$main = 'C:\xampp\htdocs\practice\demo'; // Source folder (inside this folder subfolders and inside each subfolder files are present.)

$main1 = 'C:\xampp\htdocs\practice\demomainfolder'; // Destination Folder

$dirHandle = opendir($main); // Open the source folder

while ($file = readdir($dirHandle)) { // Read what's there inside the source folder

if (basename($file) != '.' && basename($file) != '..') { // Ignore if the folder name is '.' or '..'

$folderhandle = opendir($main . '\\' . $file); // Open the Sub Folders inside the Main Folder

while ($text = readdir($folderhandle)) {

if (basename($text) != '.' && basename($text) != '..') { // Ignore if the folder name is '.' or '..'

$filepath = $main . '\\' . $file . '\\' . $text;

if (!copy($filepath, $main1 . '\\' . $text)) // Copy the files present inside the subfolders to destination folder

echo "Copy failed";

else {

$fh = fopen($main1 . '\\' . 'log.txt', 'a'); // Write a log file to show the details of files copied.

$text1 = str_replace(' ', '_', $text);

$data = $file . ',' . strtolower($text1) . "\r\n";

fwrite($fh, $data);

echo $text . " is copied
";

}

}

}

}

}

}

static function renameNewFileInFolder() { //Renames the files into desired name

$main1 = 'C:\xampp\htdocs\practice\demomainfolder';

$dirHandle = opendir($main1);

while ($file = readdir($dirHandle)) {

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

$filepath = $main1 . '\\' . $file;

$text1 = strtolower($filepath);

rename($filepath, $text1);

$text2 = str_replace(' ', '_', $text1);

if (rename($filepath, $text2))

echo $filepath . " is renamed to " . $text2 . '
';

}

}

}

}

renameNewFile::copyToNewFolder();

renameNewFile::renameNewFileInFolder();

?>

Santosh answered 2020-06-26T05:32:47Z

1 votes

$allFiles = [];

public function dirIterator($dirName)

{

$whatsInsideDir = scandir($dirName);

foreach ($whatsInsideDir as $fileOrDir) {

if (is_dir($fileOrDir)) {

dirIterator($fileOrDir);

}

$allFiles.push($fileOrDir);

}

return $allFiles;

}

Ibrahim W. answered 2020-06-26T05:33:03Z

0 votes

如果我们可以安全地删除任何名为的项目,则对John Marty发布的内容进行小幅修改。 要么 ..

function readDirs($path){

$dirHandle = opendir($path);

while($item = readdir($dirHandle)) {

$newPath = $path."/".$item;

if (($item == '.') || ($item == '..')) {

continue;

}

if (is_dir($newPath)) {

pretty_echo('Found Folder '.$newPath);

readDirs($newPath);

} else {

pretty_echo('Found File: '.$item);

}

}

}

function pretty_echo($text = '')

{

echo $text;

if (PHP_OS == 'Linux') {

echo "\r\n";

}

else {

echo "";

}

}

EastsideDeveloper answered 2020-06-26T05:33:24Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值