简体   繁体   中英

PHP' glob() to select images

In my case I have a folder with a lot of images. In my source I want to get all images of ONE product.

Eg the product ID (PID) is 12345 , in my folder I have images like:

123456789.jpg
123.jpg
1234.png
12345.png
12345-1.jpg
123456-1.bmp
12345-2.gif

The images I want to select is:

12345.png
12345-1.jpg
12345-2.gif

All other images or not from that product.

At the moment I select them like:

glob("path/to/the/images/" . $product_id . "*.*", GLOB_BRACE);

problem is.. this also brings me images like:

123456789.jpg
123456-1.bmp

Is it possible to say: bring me all images that matches the PID followed by a DOT (.) and all images that matches the PID followed by a MINUS (-) ?

I am testing it here and of course in my source since a while but can't find a solution.

我想这就是你需要的:

glob("path/to/the/images/" . $product_id . "[.-]*", GLOB_BRACE);

You could do this as an if statement, like the below

if(glob("path/to/the/images/*" . $product_id . ".*", GLOB_BRACE) 
    OR glob("path/to/the/images/" . $product_id . "-*", GLOB_BRACE)) { 
       //DO Stuff 
}

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