php開發中常常對文件進行操作,文件夾和文件的拷貝,復制等。
/** * 文件夾文件拷貝 * * @param string $src 來源文件夾 * @param string $dst 目的地文件夾 * @return bool */ function dir_copy($src = '', $dst = '') { if (empty($src) || empty($dst)) { return false; } $dir = opendir($src); dir_mkdir($dst); while (false !== ($file = readdir($dir))) { if (($file != '.') && ($file != '..')) { if (is_dir($src . '/' . $file)) { dir_copy($src . '/' . $file, $dst . '/' . $file); } else { copy($src . '/' . $file, $dst . '/' . $file); } } } closedir($dir); return true; } /** * 創建文件夾 * * @param string $path 文件夾路徑 * @param int $mode 訪問權限 * @param bool $recursive 是否遞歸創建 * @return bool */ function dir_mkdir($path = '', $mode = 0777, $recursive = true) { clearstatcache(); if (!is_dir($path)) { mkdir($path, $mode, $recursive); return chmod($path, $mode); } return true; }