【问题标题】:PHP: Show images from a folderPHP:显示文件夹中的图像
【发布时间】:2014-04-28 09:47:25
【问题描述】:

我只是想在浏览器中显示文件夹中的图片。我没有网络开发经验。下面的代码不起作用。

<!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>

这是 html 页面在浏览器上呈现时的输出:

Images from floder

Using PHP

"; echo 'Random image'."

"; } ?>

【问题讨论】:

  • 始终显示相同的图像,或者您的问题是什么?
  • 请更具体地说明问题所在。另外,请发送print_r($files) 以查看 glob() 是否有问题。
  • 索引减一,应该从0开始。
  • 这一行print $image ."&lt;br /&gt;";的输出是什么
  • 路径很可能无法很好地转换为工作 URL;检查网络服务器配置。

标签: php html directory


【解决方案1】:

更新*

我认为php 代码没有得到parsed 确保您的测试文件扩展名是php 而不是html

确保您的“Notes/”目录与同一目录中的图像文件。并添加一些验证/检查扩展等。

您可以使用scandirpathinfo'functions` 来执行此操作,如下所示:

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

【讨论】:

    猜你喜欢
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多