Georgi Antonov
Georgi Antonov

Reputation: 1641

php loop through files in subfolders in folder and get file paths

Im really new to PHP i searched google to find a correct script that loops through all subfolders in a folder and get all files path in that subfolder

<?php
    $di = new RecursiveDirectoryIterator('posts');
    foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
    echo $filename. '<br/>';
    }
?>

So i have folder 'posts' in which i have subfolder 'post001' in which i have two files

controls.png
text.txt

And the code above echos this

posts\.
posts\..
posts\post001\.
posts\post001\..
posts\post001\controls.png
posts\post001\text.txt

But i want to echo only the file paths inside these subfolders like this

posts\post001\controls.png
posts\post001\text.txt

The whole point of this is that i want to dynamically create divs for each subfolder and inside this div i put img with src and some h3 and p html tags with text equal to the .txt file so is this proper way of doing that and how to remake my php script so that i get just the file paths

So I can see the answers and they are all correct but now my point was that i need something like that

foreach( glob( 'posts/*/*' ) as $filePath ){
 //create div with javascript
  foreach( glob( 'posts/$filePath/*' ) as $file ){
      //append img and h3 and p html tags to the div via javascript
  }
  //append the created div somewhere in the html again via javascript
}

So whats the correct syntax of doing these two foreach loops in php im really getting the basics now

Upvotes: 1

Views: 2253

Answers (3)

Stefan Musarra
Stefan Musarra

Reputation: 1501

<h1>Directory Listing</h1>
<?php
/**
 * Recursive function to append the full path of all files in a
 * given directory $dirpath to an array $context
 */
function getFilelist($dirpath, &$context){
  $fileArray = scandir($dirpath);
  if (count($fileArray) > 2) {
    /* Remove the . (current directory) and .. (parent directory) */
    array_shift($fileArray);
    array_shift($fileArray);
    foreach ($fileArray as $f) {
      $full_path = $dirpath . DIRECTORY_SEPARATOR . $f;
      /* If the file is a directory, call the function recursively */
      if (is_dir($full_path)) {
        getFilelist($full_path, $context);
      } else {
        /* else, append the full path of the file to the context array */
        $context[] = $full_path;
      }
    }
  }
}
/* $d is the root directory that you want to list */
$d = '/Users/Shared';
/* Allocate the array to store of file paths of all children */
$result = array();
getFilelist($d, $result);
$display_length = false;
if ($display_length) {
    echo 'length = ' . count($result) . '<br>';
}
function FormatArrayAsUnorderedList($context) {
    $ul = '<ul>';
    foreach ($context as $c) {
      $ul .= '<li>' . $c . '</li>';
    }
    $ul .= '</ul>';
    return $ul;
}
$html_list = FormatArrayAsUnorderedList($result);
echo $html_list;
?>

Upvotes: 0

Jin
Jin

Reputation: 41

See if this works :)

$di = new RecursiveDirectoryIterator('posts');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {           
    if ((substr($file, -1) != '.') && (substr($file, -2) != '..')) {
        echo $file . '<br/>';
    }
}

Upvotes: 2

Ramon Bakker
Ramon Bakker

Reputation: 1085

Take a look at this:

<?php
$filename[] = 'posts\.';
$filename[] = 'posts\..';
$filename[] = 'posts\post001\.';
$filename[] = 'posts\post001\..';
$filename[] = 'posts\post001\controls.png';
$filename[] = 'posts\post001\text.txt';

foreach ($filename as $file) {
    if (substr($file, -4, 1) === ".") {
        echo $file."<br>";
    }
}
?>

Result:

posts\post001\controls.png
posts\post001\text.txt

What this does is checking if the 4th last digit is a dot. If so, its an extension of three letters and it should be a file. You could also check for specific extensions.

$ext = substr($file, -4, 4);
if ($ext === ".gif" || $ext === ".jpg" || $ext === ".png" || $ext === ".txt") {
    echo $file."<br>";
}

Upvotes: -1

Related Questions