Voting

: min(four, three)?
(Example: nine)

The Note You're Voting On

MetaNull
17 years ago
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";

// Replace \ by / and remove the final / if any
$root = ereg_replace( "/$", "", ereg_replace( "[\\]", "/", $sourcepath ));
// Touch all the files from the $root directory
if( false === m_walk_dir( $root, "m_touch_file", true )) {
echo
"'{$root}' is not a valid directory\n";
}

// Walk a directory recursivelly, and apply a callback on each file
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;
}

// if the path indicates a file, run touch() on it
function m_touch_file( $path ) {
echo
$path . "\n";
if( !
is_dir( $path )) {
touch( $path );
}
}

?>

<< Back to user notes page

To Top