/**
- * 操縱檔案類
- *
- * 例子:
- * FileUtil::createDir('a/1/2/3'); 測試建立檔案夾 建一個a/1/2/3檔案夾
- * FileUtil::createFile('b/1/2/3'); 測試建立檔案 在b/1/2/檔案夾下面建一個3檔案
- * FileUtil::createFile('b/1/2/3.exe'); 測試建立檔案 在b/1/2/檔案夾下面建一個3.exe檔案
- * FileUtil::copyDir('b','d/e'); 測試複製檔案夾 建立一個d/e檔案夾,把b檔案夾下的內容複寫進去
- * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 測試複製檔案 建立一個b/b檔案夾,並把b/1/2檔案夾中的3.exe檔案複製進去
- * FileUtil::moveDir('a/','b/c'); 測試移動檔案夾 建立一個b/c檔案夾,並把a檔案夾下的內容移動進去,並刪除a檔案夾
- * FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe'); 測試移動檔案 建立一個b/d檔案夾,並把b/1/2中的3.exe移動進去
- * FileUtil::unlinkFile('b/d/3.exe'); 測試刪除檔案 刪除b/d/3.exe檔案
- * FileUtil::unlinkDir('d'); 測試刪除檔案夾 刪除d檔案夾
- */
- class FileUtil {
- /**
- * 建立檔案夾
- *
- * @param string $aimUrl
- * @return viod
- */
- function createDir($aimUrl) {
- $aimUrl = str_replace('', '/', $aimUrl);
- $aimDir = '';
- $arr = explode('/', $aimUrl);
- $result = true;
- foreach ($arr as $str) {
- $aimDir .= $str . '/';
- if (!file_exists($aimDir)) {
- $result = mkdir($aimDir);
- }
- }
- return $result;
- }
/**
- * 建立檔案
- *
- * @param string $aimUrl
- * @param boolean $overWrite 該參數控制是否覆蓋原檔案
- * @return boolean
- */
- function createFile($aimUrl, $overWrite = false) {
- if (file_exists($aimUrl) && $overWrite == false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite == true) {
- FileUtil :: unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil :: createDir($aimDir);
- touch($aimUrl);
- return true;
- }
/**
- * 移動檔案夾
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite 該參數控制是否覆蓋原檔案
- * @return boolean
- */
- function moveDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
- $oldDir = str_replace('', '/', $oldDir);
- $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
- if (!is_dir($oldDir)) {
- return false;
- }
- if (!file_exists($aimDir)) {
- FileUtil :: createDir($aimDir);
- }
- @ $dirHandle = opendir($oldDir);
- if (!$dirHandle) {
- return false;
- }
- while (false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($oldDir . $file)) {
- FileUtil :: moveFile($oldDir . $file, $aimDir . $file, $overWrite);
- } else {
- FileUtil :: moveDir($oldDir . $file, $aimDir . $file, $overWrite);
- }
- }
- closedir($dirHandle);
- return rmdir($oldDir);
- }
/**
- * 移動檔案
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite 該參數控制是否覆蓋原檔案
- * @return boolean
- */
- function moveFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists($fileUrl)) {
- return false;
- }
- if (file_exists($aimUrl) && $overWrite = false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite = true) {
- FileUtil :: unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil :: createDir($aimDir);
- rename($fileUrl, $aimUrl);
- return true;
- }
/**
- * 刪除檔案夾
- *
- * @param string $aimDir
- * @return boolean
- */
- function unlinkDir($aimDir) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
- if (!is_dir($aimDir)) {
- return false;
- }
- $dirHandle = opendir($aimDir);
- while (false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($aimDir . $file)) {
- FileUtil :: unlinkFile($aimDir . $file);
- } else {
- FileUtil :: unlinkDir($aimDir . $file);
- }
- }
- closedir($dirHandle);
- return rmdir($aimDir);
- }
/**
- * 刪除檔案
- *
- * @param string $aimUrl
- * @return boolean
- */
- function unlinkFile($aimUrl) {
- if (file_exists($aimUrl)) {
- unlink($aimUrl);
- return true;
- } else {
- return false;
- }
- }
/**
- * 複製檔案夾
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite 該參數控制是否覆蓋原檔案
- * @return boolean
- */
- function copyDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
- $oldDir = str_replace('', '/', $oldDir);
- $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
- if (!is_dir($oldDir)) {
- return false;
- }
- if (!file_exists($aimDir)) {
- FileUtil :: createDir($aimDir);
- }
- $dirHandle = opendir($oldDir);
- while (false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($oldDir . $file)) {
- FileUtil :: copyFile($oldDir . $file, $aimDir . $file, $overWrite);
- } else {
- FileUtil :: copyDir($oldDir . $file, $aimDir . $file, $overWrite);
- }
- }
- return closedir($dirHandle);
- }
/**
- * 複製檔案
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite 該參數控制是否覆蓋原檔案
- * @return boolean
- */
- function copyFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists($fileUrl)) {
- return false;
- }
- if (file_exists($aimUrl) && $overWrite == false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite == true) {
- FileUtil :: unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil :: createDir($aimDir);
- copy($fileUrl, $aimUrl);
- return true;
- }
- }
- ?>
-
複製代碼另一種調用方式(非靜態調用):
- $fu = new FileUtil();
- $fu->copyFile('a/1/2/3', 'a/1/2/4');
複製代碼 |