PHP Read Sub-Directories and Loop Through Files How To

PHP read sub-directories and loop through files how to?

Use RecursiveDirectoryIterator in conjunction with RecursiveIteratorIterator.

$di = new RecursiveDirectoryIterator('path/to/directory');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
}

How to recursively iterate through files in PHP?

PHP has the perfect solution for you built in.

Example

// Construct the iterator
$it = new RecursiveDirectoryIterator("/components");

// Loop through files
foreach(new RecursiveIteratorIterator($it) as $file) {
if ($file->getExtension() == 'html') {
echo $file;
}
}

Resources

  • DirectoryIterator - Manual

PHP script to loop through all of the files in a directory?

You can use the DirectoryIterator. Example from php Manual:

<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
var_dump($fileinfo->getFilename());
}
}
?>

How to list directories, sub-directories and all files in php

Use scandir to see all stuff in the directory and is_file to check if the item is file or next directory, if it is directory, repeat the same thing over and over.

So, this is completely new code.

function listIt($path) {
$items = scandir($path);

foreach($items as $item) {

// Ignore the . and .. folders
if($item != "." AND $item != "..") {
if (is_file($path . $item)) {
// this is the file
echo "-> " . $item . "<br>";
} else {
// this is the directory

// do the list it again!
echo "---> " . $item;
echo "<div style='padding-left: 10px'>";
listIt($path . $item . "/");
echo "</div>";
}
}
}
}

echo "<div style='padding-left: 10px'>";
listIt("/");
echo "</div>";

You can see the live demo here in my webserver, btw, I will keep this link just for a second

When you see the "->" it's an file and "-->" is a directory

The pure code with no HTML:

function listIt($path) {
$items = scandir($path);

foreach($items as $item) {
// Ignore the . and .. folders
if($item != "." AND $item != "..") {
if (is_file($path . $item)) {
// this is the file
// Code for file
} else {
// this is the directory
// do the list it again!
// Code for directory
listIt($path . $item . "/");
}
}
}
}

listIt("/");

the demo can take a while to load, it's a lot of items.

Loop through a directory and its sub-directories files in liquid

The short answer to your question is YES.

Solution

The Jekyll documentation guides you through displaying an index of all your _posts. Specifically, you will want something like the following:

<ul>
{% for post in site.posts %}
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>

I have tested this with subdirectories.

Explanation

Jekyll appears to ignore any folder structure inside of _posts while generating your _site. My _site has the following structure:

_posts/
|
post1.md
subdirectory/
|
post2.md
_site/
|
2015/
|
08/
|
05/
|
post1.html
post2.html

You can clearly see here that the subdirectory does not appear anywhere in your final site.

Extra credit

You can retrieve the original directory structure using {{ post.path }} and some careful string manipulation. However, at this point it's probably easier to just set the Category attribute in your YAML front matter. The category attribute can be retrieved with {{category}}.

Purely as an academic exercise, I went ahead and retrieved the directory.

  {% capture directory %}
{% assign path = post.path | remove_first:'_posts/' | split:'/' %}
{% for folder in path %}
{% unless forloop.last %}
{{ folder }}/
{% endunless %}
{% endfor %}
{% endcapture %}
Directory: {{directory}}

You can decide if this is useful.

Listing all the folders subfolders and files in a directory using php


function listFolderFiles($dir){
$ffs = scandir($dir);

unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);

// prevent empty ordered elements
if (count($ffs) < 1)
return;

echo '<ol>';
foreach($ffs as $ff){
echo '<li>'.$ff;
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
echo '</li>';
}
echo '</ol>';
}

listFolderFiles('Main Dir');

List all the files and folders in a Directory with PHP recursive function

Get all the files and folders in a directory, don't call function when you have . or ...

Your code :

<?php
function getDirContents($dir, &$results = array()) {
$files = scandir($dir);

foreach ($files as $key => $value) {
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path)) {
$results[] = $path;
} else if ($value != "." && $value != "..") {
getDirContents($path, $results);
$results[] = $path;
}
}

return $results;
}

var_dump(getDirContents('/xampp/htdocs/WORK'));

Output (example) :

array (size=12)
0 => string '/xampp/htdocs/WORK/iframe.html' (length=30)
1 => string '/xampp/htdocs/WORK/index.html' (length=29)
2 => string '/xampp/htdocs/WORK/js' (length=21)
3 => string '/xampp/htdocs/WORK/js/btwn.js' (length=29)
4 => string '/xampp/htdocs/WORK/js/qunit' (length=27)
5 => string '/xampp/htdocs/WORK/js/qunit/qunit.css' (length=37)
6 => string '/xampp/htdocs/WORK/js/qunit/qunit.js' (length=36)
7 => string '/xampp/htdocs/WORK/js/unit-test.js' (length=34)
8 => string '/xampp/htdocs/WORK/xxxxx.js' (length=30)
9 => string '/xampp/htdocs/WORK/plane.png' (length=28)
10 => string '/xampp/htdocs/WORK/qunit.html' (length=29)
11 => string '/xampp/htdocs/WORK/styles.less' (length=30)


Related Topics



Leave a reply



Submit