| 7 file.inc | file_unmanaged_delete_recursive($path) |
| 8 file.inc | file_unmanaged_delete_recursive($path) |
Deletes all files and directories in the specified filepath recursively.
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.
Parameters
$path: A string containing either an URI or a file or directory path.
Return value
TRUE for success or if path does not exist, FALSE in the event of an error.
See also
Related topics
14 calls to file_unmanaged_delete_recursive()
File
- includes/
file.inc, line 1323 - API for handling file uploads and server file management.
Code
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);
}
$dir->close();
return drupal_rmdir($path);
}
return file_unmanaged_delete($path);
}
Login or register to post comments
Comments
Symbolic links
As it is mentioned in the code for file_unmanaged_delete(), only directories and true files will be deleted not symbolic links (unless your lucky).
<?php// We cannot handle anything other than files and directories. Log an error
// for everything else (sockets, symbolic links, etc).
?>
is_file() returns true for a symbolic link if what it points to exists. So when deleting a directory if the symbolic links comes before the file it points to in the sort order then both will be deleted, if after then the file it points to will be deleted and the symbolic link will be left behind. You will also receive an error message that the directory containing the symbolic link was not deleted since it is not empty.
Simple workaround function:
<?php
/**
* Delete a path recursively.
*
* Replacement for file_unmanaged_delete_recursive() since the latter will not
* always delete symbolic links which may be contained in a directory.
*
* @param $path
* Path to directory or file that will be recursively deleted.
*/
function file_delete_recursive($path) {
if (is_dir($path)) {
// Cycle through all entries in directory and recursively remove them.
$files = scandir($path);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
file_delete_recursive($path . '/' . $file);
}
}
// Remove the directory itself.
rmdir($path);
}
else {
// Do not check if the file exists since the check may fail if the file is
// a symbolic link and the file should always exist.
unlink($path);
}
}
?>
deletes not only visible/writable files
That comment is actually misleading:
Permissions are not checked by any functions in here, so read-only files could potentially be deleted if they are in a directory that's writable. Hidden files (which I interpret as files starting with a dot), are deleted without warning (although we do skip the . and .. loops...).