I am using PHP to move the contents of a images subfolder
GalleryName/images/
into another folder. After the move, I need to delete the GalleryName directory and everything else inside it.
I know that rmdir()
won't work unless the directory is empty. I've spent a while trying to build a recursive function to scandir()
starting from the top and then unlink()
if it's a file and scandir()
if it's a directory, then rmdir()
each empty directory as I go.
So far it's not working exactly right, and I began to think -- isn't this a ridiculously simple function that PHP should be able to do? Removing a directory?
So is there something I'm missing? Or is there at least a proven function that people use for this action?
Any help would be appreciated.
PS I trust you all here more than the comments on the php.net site -- there are hundreds of functions there but I am interested to hear if any of you here recommend one over others.
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
Recursive deletion has purpose only if the target of deletion is a folder or multiple folders. To delete files recursively means to delete the contents of the folder before deleting the folder itself. If the folder has other folders in it, it will do the same with those folders.
The rm command removes complete directories, including subdirectories and files. The rmdir command removes empty directories.
Delete all files and sub-directories in a directory: To delete all files and directories in all sub-directories, we can use recursion. Here is an example of a recursive PHP function that deletes every file and folder in a specified directory. // If it is a directory. $scan = glob(rtrim ($str, '/').'/*');
Using the is_file function to check if it is a file and not a parent directory or a sub-directory. Finally, use the unlink function, which deletes the file (if PHP has valid permissions – if not, an E_WARNING error will be thrown and the function will return a boolean FALSE value).
Here is an example of a recursive PHP function that deletes every file and folder in a specified directory. // If it is a directory. $scan = glob(rtrim ($str, '/').'/*'); After removing the directory: The directory is completely deleted.
After removing the directory: The directory is completely deleted. The function checks if the $str variable represents a path to a file then it deletes the file using the function unlink. However, if $str represents a directory, then it gets a list of all files in said directory before deleting each one.
What about this?
function rmdir_recursive($dirPath){ if(!empty($dirPath) && is_dir($dirPath) ){ $dirObj= new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::SKIP_DOTS); //upper dirs not included,otherwise DISASTER HAPPENS :) $files = new RecursiveIteratorIterator($dirObj, RecursiveIteratorIterator::CHILD_FIRST); foreach ($files as $path) $path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname()); rmdir($dirPath); return true; } return false; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With