-1

How do I check if the files exists in my array if I want to delete them with array_map + unlink:

// deletes all jpg files in that path

array_map('unlink', glob($path."/*.jpg"));

Right now if the folder does not contain any .jpg files I get an error, because there is nothing to unlink I want to catch that.

B. Desai
  • 16,414
  • 5
  • 26
  • 47
utdev
  • 3,942
  • 8
  • 40
  • 70

2 Answers2

1

Your code should be:-

$filepath = __DIR__ . "/your_dir_name/" ; // Make sure this path is correct
array_map('unlink', glob( "$filepath*.jpg") ?: []); // check folder is empty or not
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0

User in_array function to check if the value exist in array or not

Umar
  • 156
  • 5