PHP reads subdirectories and list files like?

I need to loop through all the files in subdirectories. Can you help me structure my code like this:

$main = "MainDirectory"; loop through sub-directories { loop through filels in each sub-directory { do something with each file } }; 
+45
directory loops php
Jan 6 '10 at 16:35
source share
9 answers

Use RecursiveDirectoryIterator in combination with RecursiveIteratorIterator.

 $di = new RecursiveDirectoryIterator('path/to/directory'); foreach (new RecursiveIteratorIterator($di) as $filename => $file) { echo $filename . ' - ' . $file->getSize() . ' bytes <br/>'; } 
+137
Jan 06
source share
β€” -

You probably want to use a recursive function for this if your subdirectories have subdirectories

 $main = "MainDirectory"; function readDirs($main){ $dirHandle = opendir($main); while($file = readdir($dirHandle)){ if(is_dir($main . $file) && $file != '.' && $file != '..'){ readDirs($file); } else{ //do stuff } } } 

not tested the code, but it should be close to what you want.

+10
Jan 6
source share

You need to add a path to your recursive call.

 function readDirs($path){ $dirHandle = opendir($path); while($item = readdir($dirHandle)) { $newPath = $path."/".$item; if(is_dir($newPath) && $item != '.' && $item != '..') { echo "Found Folder $newPath<br>"; readDirs($newPath); } else{ echo '&nbsp;&nbsp;Found File or .-dir '.$item.'<br>'; } } } $path = "/"; echo "$path<br>"; readDirs($path); 
+7
Aug 30 2018-12-18T00:
source share

I like glob with its wildcards:

 foreach (glob("*/*.txt") as $filename) { echo "$filename\n"; } 

Details and more complex scenarios.

But if you have a complex folder structure, RecursiveDirectoryIterator is the ultimate solution.

+5
Mar 28 2018-12-12T00:
source share

Come on, try it yourself first!

What you need:

 scandir() is_dir() 

and of course foreach

http://php.net/manual/en/function.is-dir.php

http://php.net/manual/en/function.scandir.php

+4
Jan 06 '10 at 16:40
source share

Another solution for reading with subdirectories and subfiles (set the correct folder name):

 <?php $path = realpath('samplefolder/yorfolder'); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename) { echo "$filename <br/>"; } ?> 
+2
Mar 28 '13 at 12:15
source share
  <?php 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 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 <br>"; } } } } } } 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 . '<br/>'; } } } } renameNewFile::copyToNewFolder(); renameNewFile::renameNewFileInFolder(); ?> 
+1
Jan 06 '14 at 10:37
source share
 $allFiles = []; public function dirIterator($dirName) { $whatsInsideDir = scandir($dirName); foreach ($whatsInsideDir as $fileOrDir) { if (is_dir($fileOrDir)) { dirIterator($fileOrDir); } $allFiles.push($fileOrDir); } return $allFiles; } 
+1
Mar 03 '18 at 11:44
source share

Minor changes to what John Marty published if we can safely exclude any items that are named. or..

 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 "</br>"; } } 
0
May 22 '19 at 18:06
source share



All Articles