note 30089 added to function.readdir

From: Date: Thu, 06 Mar 2003 01:34:59 +0000
Subject: note 30089 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-44913@lists.php.net to get a copy of this message
Here is a function that will recursively copy a file into every directory within a specified directory, unless the directory is in an optional third parameter, an array with a list of directory names to skip. It will also print whether it copied each item it expected to. Also, if you try to copy a file to itself in PHP, you end up with an empty file. To alleviate this, there is a fourth parameter, also optional, which is the number of levels to skip when copying files. The default is 0, which will only skip the directory specified in $dir, so that a call using the file's directory will work properly. I would not recommend calling this function with a directory higher up the tree than $file's. <?php function copyintodir ($file, $dir, $skip = array(''), $level_count = 0) { if (count($skip) == 1) { $skip = array("$skip"); } if (!@($thisdir = opendir($dir))) { print "could not open $dir<br />"; return; } while ($item = readdir($thisdir) ) { if (is_dir("$dir/$item") && (substr("$item", 0, 1) != '.') && (!in_array($item, $skip))) { copyintodir($file, "$dir/$item", $skip, $level_count + 1); } } if ($level_count > 0) { if (@copy($file, "$dir/$file")) { print "Copied $file into $dir/$file<br />\n"; } else { print "Could not copy $file into $dir/$file<br />\n"; } } } ?> You can call it using a function call such as this: copyintodir ('index.php', '.', 'images'); -- http://www.php.net/manual/en/function.readdir.php http://master.php.net/manage/user-notes.php?action=edit+30089 http://master.php.net/manage/user-notes.php?action=delete+30089 http://master.php.net/manage/user-notes.php?action=reject+30089

« previous php.notes (#44913) next »