11
dircopy
dircopy
SYNOPSIS
dircopy($from, $to, $mode = 0777)
DESCRIPTION
dircopy copie tous les fichiers ainsi que tous les répertoires et sous-répertoires contenus dans le répertoire $from vers le répertoire $to.
Les liens symboliques sont préservés.
Si $from n'existe pas ou n'est pas un répertoire, dircopy retourne false.
Si $to ou un sous-répertoire n'existe pas, il est créé avec les droits d'accès $mode.
En cas de succès, dircopy retourne true, sinon false .
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);
- }
Commentaires