简体   繁体   English

php glob()检索除一个文件外的所有文件

[英]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) { . 其中之一是“ 1.jpg”。我需要从此文件夹中检索所有照片,但“ 1.jpg”($ first)除外。据我了解,我需要类似if($ image = $ first) {。 . . } inside of foreach. 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 : $image$first不等于(!=)时,可以跳过echo

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: 或者,如果$image等于$first ,则您可以使用continue跳到下一个图像,如果您在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 />";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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