An other way to recursively walk a directory and it's content, applying a callback to each file.
Exemple: Update the last modification time of each file in a folder
<?php
clearstatcache();
$sourcepath = "C:/WINDOWS/TEMP";
$root = ereg_replace( "/$", "", ereg_replace( "[\\]", "/", $sourcepath ));
if( false === m_walk_dir( $root, "m_touch_file", true )) {
echo "'{$root}' is not a valid directory\n";
}
function m_walk_dir( $root, $callback, $recursive = true ) {
$dh = @opendir( $root );
if( false === $dh ) {
return false;
}
while( $file = readdir( $dh )) {
if( "." == $file || ".." == $file ){
continue;
}
call_user_func( $callback, "{$root}/{$file}" );
if( false !== $recursive && is_dir( "{$root}/{$file}" )) {
m_walk_dir( "{$root}/{$file}", $callback, $recursive );
}
}
closedir( $dh );
return true;
}
function m_touch_file( $path ) {
echo $path . "\n";
if( !is_dir( $path )) {
touch( $path );
}
}
?>