简体   繁体   中英

Fetch specific images from a directory with php

$dirname = "media/images/iconized/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}

With the above code I can fetch all images in a folder.But, I am not able to fetch particular images from a folder.

For example:
I have images in a folder as:

1. low_0.jpg
2. low_1.jpg
3. low_2.jpg
4. med_0.jpg
5  med_1.jpg
6. med_2.jpg

From these images how to grab only the images that starts with med ?
Thanks for your time

Change the glob() command from:

$images = glob($dirname."*.jpg");

To:

$images = glob($dirname."med_*.jpg");

The * is a wildcard; it'll fill in the gap (in this case, numbers).


Full code:

$dirname = "media/images/iconized/";
$images = glob($dirname."med_*.jpg");
foreach($images as $image) {
    echo '<img src="'.$image.'" /><br />';
}

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