- /**
- 遞迴刪除目錄及其下的所有檔案
- func: rrmdir
- */
- function rrmdir($dir) {
- if (is_dir($dir)) {
- $objects = scandir($dir);
- foreach ($objects as $object) {
- if ($object != “.” && $object != “..”) {
- if (filetype($dir.”/”.$object) == “dir”) rrmdir($dir.”/”.$object); else unlink($dir.”/”.$object);
- }
- }
- reset($objects);
- }
- }
- ?>
複製代碼附:rmdir(PHP 4, PHP 5)rmdir — 刪除目錄Report a bug 說明bool rmdir ( string $dirname )嘗試刪除 dirname 所指定的目錄。 該目錄必須是空的,而且要有相應的許可權。成功時返回 TRUE, 或者在失敗時返回 FALSE.Note: 自 PHP 5.0.0 起 rmdir() 也可用於某些 URL 封裝協議。參見Supported Protocols and Wrappers 的列表看看 rmdir() 支援哪些 URL 封裝協議。Note: 在 PHP 5.0.0 中增加了 對上下文(Context)的支援。有關 上下文(Context) 的說明參見 Stream 函數。Note: 當啟用 安全模式時, PHP 會在執行指令碼時檢查被指令碼操作的目錄是否與被執行的指令碼有相同的 UID(所有者)。參見 mkdir() 和 unlink()。
- function rrmdir($dir) {
- if (is_dir($dir)) {
- $objects = scandir($dir);
- foreach ($objects as $object) {
- if ($object != "." && $object != "..") {
- if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
- }
- }
- reset($objects);
- rmdir($dir);
- }
- }
- ?>
複製代碼This isn't my code, but just thought I would share, since it took me so long to find. This is a function to delete a folder, all sub-folders, and files in one clean move.Just tell it what directory you want deleted, in relation to the page that this function is executed. Then set $empty = true if you want the folder just emptied, but not deleted. If you set $empty = false, or just simply leave it out, the given directory will be deleted, as well.
- function deleteAll($directory, $empty = false) {
- if(substr($directory,-1) == "/") {
- $directory = substr($directory,0,-1);
- }
- if(!file_exists($directory) || !is_dir($directory)) {
- return false;
- } elseif(!is_readable($directory)) {
- return false;
- } else {
- $directoryHandle = opendir($directory);
- while ($contents = readdir($directoryHandle)) {
- if($contents != '.' && $contents != '..') {
- $path = $directory . "/" . $contents;
- if(is_dir($path)) {
- deleteAll($path);
- } else {
- unlink($path);
- }
- }
- }
- closedir($directoryHandle);
- if($empty == false) {
- if(!rmdir($directory)) {
- return false;
- }
- }
- return true;
- }
- }
- ?>
-
複製代碼A patch to previous script to make sure rights for deletion is set:
- //Delete folder function
- function deleteDirectory($dir) {
- if (!file_exists($dir)) return true;
- if (!is_dir($dir) || is_link($dir)) return unlink($dir);
- foreach (scandir($dir) as $item) {
- if ($item == '.' || $item == '..') continue;
- if (!deleteDirectory($dir . "/" . $item)) {
- chmod($dir . "/" . $item, 0777);
- if (!deleteDirectory($dir . "/" . $item)) return false;
- };
- }
- return rmdir($dir);
- }
- ?>
複製代碼更多內容,請參考 http://cn.php.net/rmdir 。 >>> |