diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-02-13 00:39:01 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-02-13 00:39:01 +0000 |
commit | 52c17c6d3ec99a872436ad09377401c25ff0bb55 (patch) | |
tree | 7305d46ec03a700f2eb4a2d9f5cf546b4944ac9f /includes | |
parent | 6afc39c2e468798f3c15e1faee45e51f1440bb51 (diff) | |
download | brdo-52c17c6d3ec99a872436ad09377401c25ff0bb55.tar.gz brdo-52c17c6d3ec99a872436ad09377401c25ff0bb55.tar.bz2 |
#373502 by drewish and sun: Add function to delete unmanaged files recurisevely (with tests).
Diffstat (limited to 'includes')
-rw-r--r-- | includes/file.inc | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/includes/file.inc b/includes/file.inc index 05318add6..6c865eeaf 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -782,6 +782,7 @@ function file_delete($file, $force = FALSE) { * error. * * @see file_delete() + * @see file_unmanaged_delete_recursive() */ function file_unmanaged_delete($path) { if (is_dir($path)) { @@ -797,11 +798,48 @@ function file_unmanaged_delete($path) { watchdog('file', 'The file %path was not deleted, because it does not exist.', array('%path' => $path), WATCHDOG_NOTICE); return TRUE; } - // Catch all for everything else: sockets, symbolic links, etc. + // We cannot handle anything other than files and directories. Log an error + // for everything else (sockets, symbolic links, etc). + watchdog('file', 'The file %path is not of a recognized type so it was not deleted.', array('%path' => $path), WATCHDOG_ERROR); return FALSE; } /** + * Recursively delete all files and directories in the specified filepath. + * + * If the specified path is a directory then the function will call itself + * recursively to process the contents. Once the contents have been removed the + * directory will also be removed. + * + * If the specified path is a file then it will be passed to + * file_unmanaged_delete(). + * + * Note that this only deletes visible files with write permission. + * + * @param $path + * A string containing a file or directory path. + * @return + * TRUE for success or path does not exist, or FALSE in the event of an + * error. + * + * @see file_unmanaged_delete() + */ +function file_unmanaged_delete_recursive($path) { + if (is_dir($path)) { + $dir = dir($path); + while (($entry = $dir->read()) !== FALSE) { + if ($entry == '.' || $entry == '..') { + continue; + } + $entry_path = $path . '/' . $entry; + file_unmanaged_delete_recursive($entry_path); + } + return rmdir($path); + } + return file_unmanaged_delete($path); +} + +/** * Determine total disk space used by a single user or the whole filesystem. * * @param $uid |