0

Can anyone suggest the best way to empty a folder using PHP. I do not want to use the recursive approach. Is there any alternative and what is that function/approach?

Thanks

dk007
  • 79
  • 2
  • 9

3 Answers3

1

Have you tried using a combination of array_map, unlink and glob like this :

array_map('unlink', glob("/path/to/folder/*"));

Credits to Stichoza's answer

Community
  • 1
  • 1
tektiv
  • 14,010
  • 5
  • 61
  • 70
0
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file))
    unlink($file); // delete file
}
SAUMYA
  • 1,508
  • 1
  • 13
  • 20
0

Use below code

array_map('unlink', glob("path/test/*"));
Brijal Savaliya
  • 1,101
  • 9
  • 19