【问题标题】:Show image and text file at once (scandir)一次显示图像和文本文件(scandir)
【发布时间】:2015-01-10 16:10:41
【问题描述】:

我遇到了问题。我需要显示图像 - 位于文件夹中 - 如果扩展名是 .png、.jpg、.jpeg 或 .gif 。如果扩展名不是其中之一,则只显示文件的内容(.tex)。

我制作了一个有效的脚本。但它只显示其中一个(img 或文本文件一次)。我需要同时显示它们。

<?php  
            $allFiles = scandir('post/'); 
            $files = array_diff($allFiles, array('.', '..'));

            foreach($files as $file)
                {   
                $ext = substr(strrchr($file, '.'), 1);
                if($ext = "jpg" || $ext = "png" || $ext = "jpeg" || $ext = "gif" ) {
                    echo "<div class=post>
                            <img width= 200px src=post/".$file.">
                        </div>";
                    }

                else {  
                echo    "<div class=post>
                            ". file_get_contents("post/".$file) ."
                        </div>";
                }

                }
         ?>  

非常感谢您的帮助。

【问题讨论】:

标签: php image text scandir


【解决方案1】:

问题已解决:将 = 更改为 ==

<?php  
        $allFiles = scandir('post/'); 
        $files = array_diff($allFiles, array('.', '..'));

        foreach($files as $file)
            {   
            $ext = substr(strrchr($file, '.'), 1);
            if($ext == "jpg" || $ext == "png" || $ext == "jpeg" || $ext == "gif" ) {
                echo "<div class=post>
                        <img width= 200px src=post/".$file.">
                    </div>";
                }

            else {  
            echo    "<div class=post>
                        ". file_get_contents("post/".$file) ."
                    </div>";
            }

            }
     ?> 

【讨论】:

    【解决方案2】:

    试试这个:

            $allFiles = scandir('post/');
            $extArray = array('jpg', 'png', 'jpeg', 'gif');
    
            foreach($allFiles as $file)
            {   
                $ext = end(explode('.', $file));
                if(in_array($ext, $extArray)) {
                    echo "<div class='post'>
                             <img width='200px' src='post/'". $file ." />
                          </div>";
                }else {  
                    echo "<div class='post'>"
                             . file_get_contents('post/'.$file)
                          ."</div>";
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多