How to display images from folder in PHP

As a web developer I always store images in database. And when I need to show them I just simple query to the database get all images and display them in html by using PHP loop.

But sometime it is not required saving images in database. You just need to show images from folder. So in this tutorial I am going to show you how to display images from folder in PHP.

Display Images from folder in PHP:

Below images are fetched from the images folder and display in inline-view.

display images from folder in PHP

If you want to add zoom effect on image hover then also read: jQuery Zoom Image on Hover

PHP Code:

Also Read: How to upload multiple images in PHP and store in Mysql

 

Posted in PHP

28 Replies to “How to display images from folder in PHP”

    1. Please download attached code and then check in localhost. If you are copying pasting above code then you need to create an images folder in root directory and add some images into images folder and then check.

      If you will face any problem feel free to contact me.

  1. I copy and paste it. but i change the directory. the image still not appear. but if i run your code. the image will appear

    1. If you are using custom image directory then please change directory name in line no. 3 and line no. 20 in the above code. Other wise paste the code here then I will check and guide you accordingly.

      1. <?php
        $imagesDirectory = "C:/xampp/htdocs/login/webcamImage";

        if (is_dir($imagesDirectory))
        {
        $opendirectory = opendir($imagesDirectory);

        while (($image = readdir($opendirectory)) !== false)
        {
        if(($image == '.') || ($image == '..'))
        {
        continue;
        }

        $imgFileType = pathinfo($image,PATHINFO_EXTENSION);

        if(($imgFileType == 'jpg') || ($imgFileType == 'png'))
        {
        echo " “;
        }
        }

        closedir($opendirectory);

        }
        ?>

        1. <?php
          $imagesDirectory = "C:/xampp/htdocs/login/webcamImage";

          if (is_dir($imagesDirectory))
          {
          $opendirectory = opendir($imagesDirectory);

          while (($image = readdir($opendirectory)) !== false)
          {
          if(($image == '.') || ($image == '..'))
          {
          continue;
          }

          $imgFileType = pathinfo($image,PATHINFO_EXTENSION);

          if(($imgFileType == 'jpg') || ($imgFileType == 'png'))
          {
          echo " “;
          }
          }

          closedir($opendirectory);

          }
          ?>

          1. What is the format of your images? Above code only works with jgp and png file. If you want to add more extension image then add condition in line 18.

          2. use

            if(($imgFileType == ‘jpg’) || ($imgFileType == ‘png’) || ($imgFileType == ‘jpeg’) );

            condition in line no. 18

          3. this image i snap first and then it will save the name of this image in database. then the image will save in folder which located in htdocs (xampp)

          4. Try below code,

            $imagesDirectory = “webcamImage/”;

            if(is_dir($imagesDirectory))
            {
            $opendirectory = opendir($imagesDirectory);

            while (($image = readdir($opendirectory)) !== false)
            {
            if(($image == ‘.’) || ($image == ‘..’))
            {
            continue;
            }

            $imgFileType = pathinfo($image,PATHINFO_EXTENSION);

            if(($imgFileType == ‘jpg’) || ($imgFileType == ‘png’))
            {
            echo ” “;
            }
            }

            closedir($opendirectory);

            }

          5. Please check source of the images and set path. Code is working fine. There is only problem with image source. If you again face the problem. Send me the complete code in zip file

  2. i’m sorry to ask how i can give you the code, because it’s only can send image only

Leave a Reply

Your email address will not be published. Required fields are marked *