How do I delete a directory and its entire contents (files and subdirectories) in PHP?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
'simple' code that works and can be read by a ten year old:
Please note that all I did was expand/simplify and fix (didn't work for non empty dir) the solution here: In PHP how do I recursively remove all folders that aren't empty?
cleanup.php :
Building on The Pixel Developer's comment, a snippet using the SPL might look like:
Note: It does no sanity checking and makes use of the SKIP_DOTS flag introduced with the FilesystemIterator in PHP 5.3.0. Of course, the
$todo
could be anif
/else
. The important point is thatCHILD_FIRST
is used to iterate over the children (files) first before their parent (folders).Something like this?
unlinkr function recursively deletes all the folders and files in given path by making sure it doesn't delete the script itself.
if you want to delete all files and folders where you place this script then call it as following
if you want to just delete just php files then call it as following
you can use any other path to delete the files as well
This will delete all files in home/user/temp directory.
I use this code ...
or this one...