/** * Copies everything from directory $fromDir to directory $toDir and * sets up files mode $chmod */ function copydir_r($fromDir, $toDir, $chmod = 0757) { // Check for some errors $errors = array(); $messages = array(); if (!is_dir($toDir)) { mkdir($toDir, $chmod); } if (!is_dir($fromDir)) { $errors[] = "source dir {$fromDir} is not a directory"; } if (!empty($errors)) { return false; } // Processing. $exceptions = array(".", "..", ".svn"); $handle = opendir($fromDir); while (false !== ($item = readdir($handle))) { if (!in_array($item, $exceptions)) { // Cleanup for trailing slashes in directories // destinations. $from = str_replace("//", "/", "{$fromDir}/{$item}"); $to = str_replace("//", "/", "{$toDir}/{$item}"); if (is_file($from)) { if (@copy($from, $to)) { chmod($to, $chmod); touch($to, filemtime($from)); // to track last modified time $messages[] = "File copied from {$from} to {$to}"; } else { $errors[] = "cannot copy file from {$from} to {$to}"; } } if (is_dir($from)) { if (@mkdir($to)) { chmod($to, $chmod); $messages[] = "Directory created: {$to}"; } else { $errors[] = "cannot create directory {$to}"; } copydir_r($from, $to, $chmod); } } } closedir($handle); return true; }
/** * Copies everything from directory $fromDir to directory $toDir and * sets up files mode $chmod */ function copydir_r($fromDir, $toDir, $chmod = 0757) { // Check for some errors $errors = array(); $messages = array(); if (!is_dir($toDir)) { mkdir($toDir, $chmod); } if (!is_dir($fromDir)) { $errors[] = 'source ' . $fromDir . ' is not a directory'; } if (!empty($errors)) { return false; } // Processing. $exceptions = array('.', '..', '.svn'); $handle = opendir($fromDir); while (false !== ($item = readdir($handle))) { if (!in_array($item, $exceptions)) { // Cleanup for trailing slashes in directories // destinations. $from = str_replace('//', '/', $fromDir . '/' . $item); $to = str_replace('//', '/', $toDir . '/' . $item); if (is_file($from)) { if (@copy($from, $to)) { chmod($to, $chmod); touch($to, filemtime($from)); // to track last modified time $messages[] = 'File copied from ' . $from . ' to ' . $to; } else { $errors[] = 'cannot copy file from ' . $from . ' to ' . $to; } } if (is_dir($from)) { if (@mkdir($to)) { chmod($to, $chmod); $messages[] = 'Directory created: ' . $to; } else { $errors[] = 'cannot create directory ' . $to; } copydir_r($from, $to, $chmod); } } } closedir($handle); return true; }