在解决了这问题之后。
我的代码:-
<?php for ($i = 1; $i <= 9; $i++):?>
<div class="col-md-2">
<div class="thumbnail">
<a href="<?php echo $details . $i;?>.php">
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
</a>
</div>
</div>
<?php endfor; ?> 现在假设现在有图像作为8 and ,所以for loop应该跳过它并转到next,而不显示任何图像。
不应显示红色部分。
发布于 2017-09-02 09:46:05
由于您在评论中声明图像的URL为localhost/downloads/vf/images,所以我将假设使用$_SERVER['DOCUMENT_ROOT']可以处理公共文件夹之外的文件夹(这可能类似于C:\users\chirag\htdocs\,但$_SERVER变量应该负责这一点。如果不是这样的话,找到完整的路径并使用它--因为file_exists()需要系统路径,而不是公共路径/URL。这还假设您对$images变量使用相对路径,这意味着您有$images = "/downloads/vf/images/";而不是$images = "localhost/downloads/vf/images/";。
有了这个假设,我们现在可以使用file_exists()了,因为这需要系统路径,而不是URL。
<?php
$images = "/downloads/vf/images/";
for ($i = 1; $i <= 9; $i++):
if (!file_exists($_SERVER['DOCUMENT_ROOT'].$images.$i.".png"))
continue; // Skip this iteration if it can't find the file
?>
<div class="col-md-2">
<div class="thumbnail">
<a href="<?php echo $details . $i;?>.php">
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
</a>
</div>
</div>
<?php endfor; ?> continue;$_SERVERfile_exists()发布于 2017-09-02 09:05:23
如果映像存在或不存在,则必须添加检查条件,有某些方法(如file_exists )
<?php for ($i = 1; $i <= 9; $i++):
if(file_exists("file_path")){ ?>
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
<? { php endfor; ?>这可能对你有帮助:如何检查图像是否存在
发布于 2017-09-02 09:06:32
使用此代码
File_exists是检查文件是否存在的函数。
<?php for ($i = 1; $i <= 9; $i++):
if (file_exists($images.$i.'.png')) { ?>
<img src="<?php echo $images . $i;?>.png" alt="<?php echo $i;?>">
<?php } endfor; ?>查看下面的内容以获得更多细节。
https://stackoverflow.com/questions/46012150
复制相似问题