Php 如何显示每个文件夹的第一个图像

Php 如何显示每个文件夹的第一个图像,php,Php,我已经搜索了堆栈溢出,只有以下代码正确显示了文件夹的图像(我已经尝试过)。有两个问题: 它会为前两个文件夹分别添加一个和一个。,因此,如果($h=-1)和($h==0)) 它不显示第二个文件夹的第一个图像,而是显示变量$h中的图像。由于某种原因,变量$h在完成foreach循环时似乎没有重置为-1。相反,我认为第一个阵列的图像正在显示,因此输出为: Album_1,该文件夹中的第一张图像, Album_2,来自Album_1文件夹的第一张图像 $dirs = array_filter(glob(

我已经搜索了堆栈溢出,只有以下代码正确显示了文件夹的图像(我已经尝试过)。有两个问题:

  • 它会为前两个文件夹分别添加一个
    和一个
    ,因此,如果($h=-1)和($h==0))

  • 它不显示第二个文件夹的第一个图像,而是显示变量
    $h
    中的图像。由于某种原因,变量
    $h
    在完成foreach循环时似乎没有重置为
    -1
    。相反,我认为第一个阵列的图像正在显示,因此输出为:

  • Album_1
    ,该文件夹中的第一张图像,
    Album_2
    ,来自
    Album_1
    文件夹的第一张图像

    $dirs = array_filter(glob('*'), 'is_dir');
    
    foreach($dirs as $dirk) {
        $h = -1;
        //echo 'hey'.$dirk.'heya';
        $dir = $dirk;
        $dh = opendir($dir);
        while (false !== ($filename = readdir($dh))) {
            $files[] = $filename;
        }
    
        $images = preg_grep ('/\.jpg$/i', $files);
    
        echo '<div class="div_class">';
        foreach($files as $image) {
            if (($h == -1) && ($h == 0)) {
                echo '';
            }
    
            if ($h == 1) {
                echo '
                        <input type="checkbox" id="'.$image.'" name="album" 
                         value="'.$image.'" class="checkbox_class"/>
                        <label for="'.$image.'" class="label_class">
                            <div class="div_inside_label">
                                <span>'.$dirk.'</span>
                                <br>
                                <span>'.$h.'</span>
                            </div>
                        </label>
                        <img class="table_images"
                         src="http://localhost/refit/code/'.$dir.'/
                         '.$image.'"/>
                    ';
            }   
            else {
                echo '';
            }
    
            $h++;
        }
    
        echo '</div>';
        echo $h;    
    }   
    
    $dirs=array_filter(glob('*'),'is_dir');
    外汇($dirs作为$dirk){
    $h=-1;
    //echo‘嘿’。$dirk‘嘿’;
    $dir=$dirk;
    $dh=opendir($dir);
    while(false!=($filename=readdir($dh))){
    $files[]=$filename;
    }
    $images=preg\u grep('/\.jpg$/i',$files);
    回声';
    foreach($image形式的文件){
    如果($h==1)和($h==0)){
    回声';
    }
    如果($h==1){
    回声'
    “.$德克。”
    
    “.$h.” '; } 否则{ 回声'; } $h++; } 回声'; echo$h; }
    尝试以下方法:

    # Nothing really different here
    $dirs = array_filter(glob('*'), 'is_dir');
    echo '<div class="div_class">';
    foreach($dirs as $dirk) {
        # Glob the folder for a jpg
        foreach(glob($dirk.'/*.jpg') as $image) {
            echo '
                <input type="checkbox" id="'.$image.'" name="album" value="'.$image.'" class="checkbox_class"/>
                <label for="'.$image.'" class="label_class">
                    <div class="div_inside_label">
                        <span>'.$dirk.'</span>
                    </div>
                </label>
                <!-- Here you will probably need to fix the path -->
                <img class="table_images" src="'.$image.'" />
            ';
            # Stop in this folder
            break;
        }
    }
    echo '</div>';
    
    #这里没有什么不同
    $dirs=array_filter(glob('*'),'is_dir');
    回声';
    外汇($dirs作为$dirk){
    #Glob用于jpg的文件夹
    foreach(glob($dirk./*.jpg')作为$image){
    回声'
    “.$德克。”
    ';
    #停止在此文件夹中
    打破
    }
    }
    回声';
    
    您可能想这样尝试一下:
    $directories=array_diff(scandir($directory),array(“…”,“)以避免显示点目录。否则,我不能理解你的代码太多,太混乱,我太累了。立即工作。谢谢你!我可以问一下,你将如何显示.png.gif.jpeg和其他格式?@user9605523看一看。