Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete all files in a folder, which matches a certain pattern?

Tags:

file

php

unlink

I have a folder with images.

As example:

z_1.jpg
z_2.jpg
z_3.jpg
//...

I want to delete every image with prefix z_*.jpg. How can I do that?

unlink('z_*.jpg'); ?
like image 742
Arriba Avatar asked Nov 19 '25 14:11

Arriba


1 Answers

You need the exact filename to unlink() a file. So just use glob() to get all files which you want to grab. Loop through the returned array and delete the files, e.g.

<?php

    $files = glob("z_*.jpg");

    foreach($files as $file)
        unlink($file);

?>
like image 129
Rizier123 Avatar answered Nov 22 '25 03:11

Rizier123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!