简体   繁体   English

PHP的全球不返回所有文件

[英]php glob not returning all files

I looked at similar questions here and around the web, but none of the solutions have worked. 我在这里和网络上都看过类似的问题,但是没有一个解决方案有效。

I'm using glob to return 177 images from a folder. 我正在使用glob从文件夹返回177张图像。 Only some return. 只有一些回报。 Sometimes nothing returns. 有时什么也没回来。 Every time I reload the page, a few more images are loaded. 每次我重新加载页面时,都会加载更多图像。 In the inspector, everything looks like it's loaded; 在检查器中,所有内容看起来都已加载。 the entire code is how it should be. 整个代码应该是这样。 But looking at the page, I can see that there are obviously images missing. 但是看一下页面,我可以看到显然缺少图像。

Here's my code: 这是我的代码:

<?php

    set_time_limit(0);
    ignore_user_abort(1);

    $images = glob("images/pics/*", GLOB_BRACE);
    foreach ($images as $image) { ?>

    <div class='img_container_2 backing_center' style='background: none'>
        <img style='width: 400px' src = '<?php echo $image; ?>'/>
    </div>
<?php

    }

?>

I was thinking it might be a time-out problem, but setting the limit to 0 doesn't seem to make a difference. 我以为这可能是超时问题,但是将限制设置为0似乎没有什么不同。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

PHP glob() function works perfectly in your code. PHP glob()函数可在您的代码中完美运行。

There is a problem with your directory: 您的目录存在问题:

  1. Be sure your folder contains images only 确保您的文件夹包含图像
  2. Remove Thumbs.db in your $images array $images数组中删除 Thumbs.db

Remove Thumbs.db 删除Thumbs.db

$key = array_search('Thumbs.db', $images );
$new_images = unset($images[$key]);

To Find Images only 仅查找图像

$images = glob("images/pics/*.jpg", GLOB_BRACE);

Note : If your folder only contains images, then you don't have to use .jpg in glob function. 注意 :如果您的文件夹仅包含图像,则无需在glob函数中使用.jpg

You specify GLOB_BRACE as the 2nd argument for glob in order for it to work. 您可以将GLOB_BRACE指定为glob的第二个参数,以使其起作用。

So for example, if you executed glob("{a,b,c}.php", GLOB_BRACE) on the following list of files: 因此,例如,如果您在以下文件列表上执行了glob("{a,b,c}.php", GLOB_BRACE)

a.php

b.php

c.php 

Replace GLOB_BRACE with GLOB_NOSORT (Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically) GLOB_BRACE替换为GLOB_NOSORT (返回目录中显示的文件(不进行排序)。不使用此标志时,路径名按字母顺序排序)

For more details see globe flags 有关更多详细信息, 请参见地球标志

My bad, I did a dumb thing. 我的坏,我做了愚蠢的事。 I set the width to an absolute value when it should have been set to a relative value. 当应该将宽度设置为相对值时,我将宽度设置为绝对值。 I'm still not sure why only some images were showing and some weren't, but at least it's fixed now. 我仍然不确定为什么只显示一些图像而没有显示,但至少现在已修复。 Thanks anyway for the help, everyone! 谢谢大家的帮助!

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

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