Question

I have an array which contains a number of file names without extensions (below, the numbers are the name of the files):

[1] => 214
[2] => 12
[3] => 2763
[4] => 356
[5] => 87

I would like to delete these files from the server without knowing their extensions (which could be .jpg .JPG .GIF .jpeg, etc).

I tried using GLOB but I don't understand how to teach PHP to use wildcards and in the same time get the name of the files from the array.

Was it helpful?

Solution

Assuming $names is your array:

$path = "../pictures/";
foreach ($names as $name) {
  foreach (glob($path . $name . '*') as $filename) {
    unlink(realpath($filename));
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top