Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A recursive remove directory function for PHP?

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.

like image 626
rhodesjason Avatar asked Sep 10 '09 19:09

rhodesjason


People also ask

How do you recursively delete a directory?

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.

What is recursive remove?

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.

Will rm remove directory?

The rm command removes complete directories, including subdirectories and files. The rmdir command removes empty directories.

How to delete all files and sub-directories in a PHP Directory?

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, '/').'/*');

How do I delete a file in PHP?

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).

How to delete every file and folder in a specified directory?

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.

What happens when you remove a directory in Linux?

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.


1 Answers

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; } 
like image 70
barbushin Avatar answered Oct 06 '22 18:10

barbushin