Search code examples
phpdelete-fileunlink

Delete a file which I only know the first part of the name in PHP


I want to delete a file which I only know the first part of the name.

    unlink('./upload/nav_thumbs/project-'  .the rest);

'the rest' = can be anything. Only thing I got is that is has a .jpg extension

Is there a way to do this?


Solution

  • $possibleFiles = glob('./upload/nav_thumbs/project-*.jpg');
    foreach ($possibleFiles as $file) {
        if (thisIsTheFileYouAreLookingFor($file)) {
            unlink($file);
        }
    }
    

    http://php.net/glob