萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php刪除指定目錄的方法

php刪除指定目錄的方法

 本文實例講述了php刪除指定目錄的方法。分享給大家供大家參考。具體分析如下:

這段代碼可實現遞歸刪除子目錄的功能

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <?php /** * Delete a file, or a folder and its contents * (recursive algorithm) * @author Aidan Lister <[email protected]> * @version 1.0.3 * @param string $dirname Directory to delete * @return bool Returns TRUE on success, FALSE on failure */ function rmdirr($dirname) { // Sanity check if (!file_exists($dirname)) { return false; } // Simple delete for a file if (is_file($dirname) || is_link($dirname)) { return unlink($dirname); } // Loop through the folder $dir = dir($dirname); while (false !== $entry = $dir->read()) { // Skip pointers if ($entry == '.' || $entry == '..') { continue; } // Recurse rmdirr($dirname . DIRECTORY_SEPARATOR . $entry); } // Clean up $dir->close(); return rmdir($dirname); } ?>
copyright © 萬盛學電腦網 all rights reserved