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