11
dircopy
dircopy
SYNOPSIS
dircopy($from, $to, $mode = 0777)
DESCRIPTION
dircopy copies all the files and all the directories and subdirectories contained in the directory $from to the directory $to.
Symbolic links are preserved.
If $from does not exist or is not a directory, dircopy returns false.
If $to or a subdirectory does not exist, it is created with the access rights $mode.
In case of success, dircopy returns true, or false otherwise.
CODE
- function dircopy($from, $to, $mode = 0777) {
- if (!is_dir($from)) {
- return false;
- }
- if (!is_dir($to)) {
- if (!@mkdir($to, $mode)) {
- return false;
- }
- }
- dircopyaux($from, $to, $mode);
- return true;
- }
- function dircopyaux($from, $to, $mode) {
- $handle = opendir($from);
- while (($file = readdir($handle)) !== false) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- $frompath = $from . DIRECTORY_SEPARATOR . $file;
- $topath = $to . DIRECTORY_SEPARATOR . $file;
- if (is_link($frompath)) {
- if (file_exists($topath)) {
- unlink($topath);
- }
- symlink(readlink($frompath), $topath);
- }
- else if (is_file($frompath)) {
- copy($frompath, $topath);
- }
- else if (is_dir($frompath)) {
- if (!is_dir($topath)) {
- mkdir($topath, $mode);
- }
- dircopyaux($frompath, $topath, $mode);
- }
- }
- closedir($handle);
- }
Comments