简体   繁体   English

PHP:显示文件夹中的图像

[英]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: 这是在浏览器上呈现时html页面的输出:

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 我认为未parsed php代码,请确保您的测试文件扩展名是php而不是html

Make sure you have 'Notes/` directory with image files in same directory. 确保您拥有'Notes /`目录,并且图像文件位于同一目录中。 And also add some validation / check extension etc. 并添加一些验证/检查扩展名等

You can use scandir and pathinfo' functions` to do this as below: 您可以使用scandirpathinfo'功能来做到这一点,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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