note 30087 deleted from function.copy by cece

From: Date: Tue, 19 Aug 2003 12:33:02 +0000
Subject: note 30087 deleted from function.copy by cece
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54659@lists.php.net to get a copy of this message
Note Submitter: ---- 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. NEW: Apparently, if you try to copy a file to itself in PHP, you end up with an empty file. To alleviate this, I added 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 the 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');

« previous php.notes (#54659) next »