简体   繁体   中英

PHP: Show images from a folder

I am just trying to display pictures from a folder in a browser. I have no web development experience. The below code is not working.

<!DOCTYPE html>
<html>
<body>

<h1>Images from floder</h1>
<p>Using PHP</p>

<?php

   $files = glob("Notes/*.*");

   for ($i=1; $i<count($files); $i++)

  {

  $image = $files[$i];

  print $image ."<br />";
  echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
   }

 ?>

</body>
</html>

This was the output of the html page when rendered on a browser:

Images from floder

Using PHP

"; echo 'Random image'."

"; } ?>

Update*

I think php code is not getting parsed make sure your test file extension is php not html

Make sure you have 'Notes/` directory with image files in same directory. And also add some validation / check extension etc.

You can use scandir and pathinfo' functions` to do this as below:

$dir = 'Notes/';
$files = scandir($dir);
$allowed_extensions = array('png', 'gif', 'jpg', 'jpeg');
foreach ($files as $file) {
    if (in_array(pathinfo($file, PATHINFO_EXTENSION), $allowed_extensions)) {
        echo '<img src="' . $dir . $file . '" alt="Random image" />' . "<br /><br />";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM