note 20738 deleted from function.copy by alindeman

From: Date: Fri, 09 May 2003 22:16:26 +0000
Subject: note 20738 deleted from function.copy by alindeman
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-47793@lists.php.net to get a copy of this message
improved: rec_copy (see above) copy2(string $src_path, $dest_path, $recursive = false, $overwrite = false) if you want to do a recursive copy and overwrite existing files, do: copy2($src_path, $dest_path, 1, 1); relative path (like "../../some_path") is possible too.. not tested. should work. function unix_path($path){ $path = ereg_replace("[\\]", "/", $path); //dos path to unix if (is_dir($path) && substr($path, -1) != "/") $path .= "/"; //add trailing slash if not present return $path; } function full_path($path){ // input path like: "../../some_path" $path = unix_path($path); $cnt = 0; while (ereg("^(\.\.?)/?(.*)$", $path, $regs)){ if ($regs[1] == "..") $cnt++; $path = $regs[2]; } if (!$cnt) return $path; $cur = unix_path(getcwd()); for ($i = 0; $i < $cnt; $i++){ if (!ereg("^(.*/)[^/]*/$", $cur, $regs)) return false; $cur = $regs[1]; } return $cur. $path; } function copy2($src, $dest, $recursive = false, $overwrite = false){ if (!($src = full_path($src))) // convert relative path to full path return false; if (!($dest = full_path($dest))) return false; if (is_dir($src)) if (!copy2_rec($src, $dest, $recursive, $overwrite)) return false; if (is_file($src) && ($overwrite || !file_exists($dest))) // do regular copy if (!copy($src, $dest)) return false; return true; } function copy2_rec($src, $dest, $recursive, $overwrite){ if (!file_exists($dest)) if (!mkdir($dest, 0755)) return false; $handle = opendir($src); while ($file = readdir($handle)){ $src_file = $src. $file; $dest_file = $dest. $file; if ($recursive && is_dir($src_file) && $file != "." && $file != "..") if (!copy2_rec($src_file. "/", $dest_file. "/", $recursive, $overwrite)) return false; if (is_file($src_file) && ($overwrite || !file_exists($dest_file))) if (!copy($src_file, $dest_file)) return false; } closedir($handle); return true; }

« previous php.notes (#47793) next »