简体   繁体   中英

Match files in directory with glob and regex

I had a problem with glob in php. First I'm create thumbs dynamically and file named as "thumb_width_heigth_filename.ext" Then i had to delete all created thumbs with "filename.ext" its not complicated. I use:

$filename = "name.jpg";
glob(realpath($path)."/thumb_[0-9]*_[0-9]*_$filename")

its work fine for me, but. if I have files like:

name.jpg
thumb_20_20_name.jpg
thumb_200_200_name.jpg

and I'm also have files like:

QQ_name.jpg (because system automatically add random 2 symbols if file already exist in folder)
thumb_20_20_QQ_name.jpg

Then I want to delete name.jpg and cleanup thumbs I use my code, and got all files matching include QQ_name

I know howto delete files, help me, please, exlude files like QQ_name.jpg when I'm search thumb_[0-9] _[0-9]*_name.jpg

Please help find out.

This finds every file, that has $filename at the end of the $filename. With array_map() and unlink() you can delete all these files very simple.

array_map('unlink', glob('path/*'.$filename));

This will delete

name.jpg
thumb_20_20_name.jpg
thumb_200_200_name.jpg
QQ_name.jpg
thumb_20_20_QQ_name.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM