Php foreach文件夹回显foreach图像

Php foreach文件夹回显foreach图像,php,html,foreach,Php,Html,Foreach,我现在正在用这个 foreach (glob('img/gallery/*') as $dir) { $folder = str_replace('img/gallery/', '', $dir); foreach (glob($dir.'/*.jpg') as $img) { $img1 = str_replace('img/gallery/'.$folder.'/', '', $img); $img1 = str_replace('.jpg'

我现在正在用这个

foreach (glob('img/gallery/*') as $dir) {
    $folder = str_replace('img/gallery/', '', $dir);
    foreach (glob($dir.'/*.jpg') as $img)  {
        $img1 = str_replace('img/gallery/'.$folder.'/', '', $img);
        $img1 = str_replace('.jpg', '', $img1);
        $img = str_replace(' ', '%20', $img);?>
        <a class="portfolio" title="<?=$img1?>" href="<?=$img?>">
            <li class="mix <?=$folder?>" 
                data-cat="<?=$folder?>" 
                style="height:145px;width:145px;float:left;margin-right:2.5px;margin-left:2.5px;margin-bottom:5px; background:url(<?=$img?>);background-size:cover;">
            </li>
        </a>
    <? }
}
foreach(glob('img/gallery/*')作为$dir){
$folder=str_replace('img/gallery/',''$dir);
foreach(glob($dir./*.jpg')作为$img){
$img1=str_replace('img/gallery/'.$folder.'/',''.$img');
$img1=str_替换('.jpg',''$img1);
$img=str_replace('',%20',$img);?>
试试这个

$images = glob($dir.'/*.jpg');
if (count($images))
foreach($images as $img) {
试试这个

$images = glob($dir.'/*.jpg');
if (count($images))
foreach($images as $img) {

您的子文件夹中是否有任何文件?例如,
/img/gallery/somefile
,然后您尝试使用
img/gallery/somefile/*.jpg
再次对其进行全局搜索,这将导致第二次全局搜索失败,返回布尔值false并触发错误消息

您需要检查您在内部全球化的内容是否实际上是一个dir,例如

foreach(glob(...) as $dir) {
    if (issdir($dir)) {
         ...
    }
}

您的子文件夹中是否有任何文件?例如,
/img/gallery/somefile
,然后您尝试使用
img/gallery/somefile/*.jpg
再次对其进行全局搜索,这将导致第二次全局搜索失败,返回布尔值false并触发错误消息

您需要检查您在内部全球化的内容是否实际上是一个dir,例如

foreach(glob(...) as $dir) {
    if (issdir($dir)) {
         ...
    }
}

在第一个foreach中使用
GLOB_ONLYDIR
作为标志,以仅获取目录:

foreach (glob('img/gallery/*',GLOB_ONLYDIR) as $dir) {

在第一个foreach中使用
GLOB_ONLYDIR
作为标志,以仅获取目录:

foreach (glob('img/gallery/*',GLOB_ONLYDIR) as $dir) {

谢谢,但是现在:警告:为index.php中的foreach()提供的参数无效,现在?我错过了$Thanks,但是现在:警告:为foreach()提供的参数无效在index.php的第191行和现在?我错过了$当然,当我读到它点击的第一个问题时,其中一个文件夹是空的,因此它不会返回任何结果!!这是漫长的一天!!当然,当我读到它点击的第一个问题时,其中一个文件夹是空的,因此它不会返回任何结果!!这是漫长的一天!!