简体   繁体   中英

php glob() retrieve all files except one

I have a folder named 'Folder'. There are several photos inside it. One of them is "1.jpg".. I need to retrieve all the photos from this folder, except "1.jpg" ($first).. As I understand, I need something like if ($image=$first) { . . . } inside of foreach.

 $first="1.jpg";
 $dirname="folder";
 $images = glob($dirname.'*');

 foreach($images as $image) {
 $html="<img src='".$image."'><br />";
 echo $html;
 }

Thanks for attention

You can skip the echo when $image is not equal (!=) to $first :

foreach($images as $image) {
    if ($image != $first) {
        $html="<img src='".$image."'><br />";
        echo $html;
    }
}

Or you can use continue to skip to the next image, when $image is equal to $first , if you have more complex code in the foreach:

foreach($images as $image) {
    if ($image == $first) {
        continue;
    }
    $html="<img src='".$image."'><br />";
    echo $html;
}
$first="1.jpg";
$dirname="folder";
$images = glob($dirname.'*');
unset($images[$first]);
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