PHP檔案及檔案夾操作之建立、刪除、移動、複製_php執行個體

來源:互聯網
上載者:User

建立FileUtil.php檔案,內容及調用方式如下:

<?php/*** 操縱檔案類* * 例子:* 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');

以上所述是小編給大家介紹的PHP檔案及檔案夾操作之建立、刪除、移動、複製,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.