简体   繁体   中英

PHP - nonrecursive deletion of all files and folders in a specified folder

I was using this php code to delete all files in a specified folder:

array_map('unlink', glob($path));

Now the structure has changed - folders were added (containing files and other folders) and this code doesn't work on them. Is there a nonrecursive solution to delete everything from a specified folder (but not the folder itself)?

Be sure to target the files only by filtering them with is_file :

array_map('unlink', array_filter(glob($path), 'is_file'));

EDIT: perhaps in order not to delete the folder itself, you need to skip . and .. - just filter them out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM