Recursively Loop Over a Directory using RecursiveDirectoryIterator

You can use PHP’s built in directory iterator (in php 5 or 7) to loop over a directory and perform operations on the files easily. This code is also checking to make sure that $file is indeed a file with the isFile() method.

// path you'd like to loop over
$folderPath = "/home/myfiles/20161212";

$dir_iterator = new RecursiveDirectoryIterator($folderPath);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
	if ($file->isFile()) {