Search code examples
phpfilealphabeticalunlink

How to take first file name from a folder and delete it in PHP


I try to make a gallery. In a folder I have some duplicate pictures. I have pictures named: af_160112, af_160113, af_160114. I would like remove this first one. How to take the first picture in a folder and delete it? So far I have known that I should use unlinke($file) function. Thank you for your help.


Solution

  • Solved.

    I used:

    $files = glob($path_to_gallery . '/*.{jpg,png,gif}', GLOB_BRACE);
       foreach($files as $file) {
       unlink($file);
       break;
       }