note 78261 added to function.opendir
| From: | MetaNull at osu1 dot php dot net | Date: | Thu, 04 Oct 2007 18:39:08 +0000 |
| Subject: | note 78261 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131087@lists.php.net to get a copy of this message | ||
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 );
}
}
?>
----
Server IP: 87.238.162.147
Probable Submitter: 85.201.3.242
----
Manual Page -- http://www.php.net/manual/en/function.opendir.php
Edit -- https://master.php.net/note/edit/78261
Del: integrated -- https://master.php.net/note/delete/78261/integrated
Del: useless -- https://master.php.net/note/delete/78261/useless
Del: bad code -- https://master.php.net/note/delete/78261/bad+code
Del: spam -- https://master.php.net/note/delete/78261/spam
Del: non-english -- https://master.php.net/note/delete/78261/non-english
Del: in docs -- https://master.php.net/note/delete/78261/in+docs
Del: other reasons-- https://master.php.net/note/delete/78261
Reject -- https://master.php.net/note/reject/78261
Search -- https://master.php.net/manage/user-notes.php