如何使用glob来查找缩略图

时间:2015-05-20 14:49:11

标签: php regex glob

假设我上传了一个名为myimage.jpg的文件,我生成了这个图像的缩略图,它将宽度x高度附加到文件名。我可能有一个看起来像这样的图像目录:

myimage.jpg
myimage-100x50.jpg
myimage-500x250.jpg
myimage-430x300.jpg

当我想删除此图像时,我还需要删除所有的拇指。我会使用glob来查找原始图像和所有缩略图,但不能找到目录中的任何其他图像吗?

2 个答案:

答案 0 :(得分:2)

我认为这可能适合你。

$images = glob('myimage*.jpg');
foreach($images as $image):
    delete($image); //whatever function you use to delete
endforeach;

答案 1 :(得分:0)

我最后这样做了:

// find all images that match the filename
foreach (glob(myimage.*) as $filename) {

    // use regex to filter out any that have a similar name and are not thumbs
    if(preg_match("/(\myimage-\b)[0-9]\d*x[0-9]\d*(\b.jpg\b)/", $filename)) {

        // delete the thumb
        unlink($filename);
    }
}

// delete the original image
unlink(myimage.jpg);