【问题标题】:PHP img src not being echoPHP img src 不是回显
【发布时间】:2018-08-14 09:11:12
【问题描述】:

我有一个文件夹,其路径为 /xampp/htdocs/pic ,其中有一些图像。

我正在使用以下代码来回显图像。但是图像没有显示。 不知道我在哪里犯了错误。

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>Listing the contents of a directory</title>
<link rel=”stylesheet” type=”text/css” href=”common.css” />
</head>
<body>
<h1>Listing the contents of a directory</h1>
<?php
   $dir = opendir("/xampp/htdocs/pic");
   while (($file = readdir($dir)) !== false) {



echo '<img src="<?php echo "/xampp/htdocs/pic/".$file."</br>";?>"/>';

   }
   closedir($dir);
?>

</body>
</html>

【问题讨论】:

  • echo "&lt;img src='/xampp/htdocs/pic/$file'/&gt;";
  • 如果您尝试获取图像的路径是正确的,下面的代码应该可以工作。将echo '&lt;img src="&lt;?php echo "/xampp/htdocs/pic/".$file."&lt;/br&gt;";?&gt;"/&gt;'; 更改为echo '&lt;img src="/xampp/htdocs/pic/'.$file.'"/&gt;';
  • 要扩展@Fky 的评论,与 JavaScript 不同,引号的 type 在 PHP 中很重要 - 请参阅手册中的 Strings。但是,我猜您的 docroot 是 /xampp/htdocs/,所以 echo "&lt;img src='/pic/$file'/&gt;"; 更有可能工作。

标签: php


【解决方案1】:

您的 PHP 只是生成 HTML 文档,然后将其发送到客户端。图像的源分辨率将由客户端的浏览器处理。因此,您必须像这样引用您的公用文件夹:

src="./pic/$file"

另外,您的 HTML 中有错误。一个 void/empty 元素不能包含另一个元素。因此,您必须编辑您的回显字符串并删除&lt;br/&gt;

echo "&lt;img src='./pic/$file' /&gt;";

这有点过头了,但每个HTML image element 还应该包含一个替代文本属性来描述图片,如下所示:

echo "&lt;img src='./pic/$file' alt='my image title' /&gt;";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 2012-11-26
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    相关资源
    最近更新 更多