-1

I got a problem. I need to show image - located in folder - if extencion is .png, .jpg, .jpeg or .gif . If extencion is not one of them then just show content of the file (.tex).

I made a script that works. But it shows just one of them (img or text file at once). I need show both of them at once.

<?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>";
                }

                }
         ?>  

Thanks a lot for help.

Lubov
  • 1

2 Answers2

0

Problem solved: change = to ==

<?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>";
            }

            }
     ?> 
Lubov
  • 1
0

Try this :

        $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>";
            }

        }