0

In my directory there are some files; I want to delete files which just their name is for example: 22, and it makes no difference what are their format !

for example there are these files in directory :

21.pdf
22.doc
22.pdf
22.docx

in normal way I use this code :

$file_name = '22.doc';
if(is_writable(dirname(__FILE__) . '/admin-upload/' . $file_name))
{
    unlink(dirname(__FILE__) . '/admin-upload/' . $file_name ); 
}

but it just delete doc , now how can I tell to program : Delete all files which their name is : 22 ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SoheilYou
  • 111
  • 1
  • 1
  • 16

1 Answers1

1

It should be ok with glob :

$fileName = '22';

foreach (glob(dirname(__FILE__) . '/admin-upload/' . $fileName .'.*') as $file) {
            unlink($file);
}
Sofiane Sadi
  • 357
  • 1
  • 7