/** * Prepare a file for writing * * @param string $path An absolute path to the file to write * @param octal $dirPerms Permissions for new directories * @return boolean|string * @return boolean:true The path is ready for writing * @return string An error message indicating where the prepare failed */ public static function prepare($path, $dirPerms = 0775) { if (file_exists($path)) { return Path::test($path, self::WRITABLE); } return Dir::prepare(dirname($path), $dirPerms); }
/** * Recursively copy a directory * * @param string $source The absolute directory path to copy from * @param string $dest The absolute directory path to copy to * @param octal $mode Permissions for newly created directories * @return boolean|string * @return boolean:true The copy was successful * @return string An error message describing the failure */ public static function copy($source, $dest, $mode = 0775) { if (($test = Dir::prepare($dest, $mode)) !== true) { return "failed to copy '{$source}' to '{$dest}'; {$test}"; } else { if (Dir::isEmpty($dest) === false) { return "failed to copy '{$source}'; '{$dest}' is not empty"; } } $iterator = Find::getIterator($source, Find::RECURSIVE); foreach ($iterator as $file) { $destPath = $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); if ($file->isDir()) { mkdir($destPath, $mode); } else { copy($file, $destPath); } } return true; }