这是我一直存在的一个非常奇怪的问题。
图像似乎有一个类似于其他名称的图像
例如,如果我有一个名为image0001.png
的图像,并且我尝试显示一个源image0010.png
的图像 - 它不存在 - 则图像不显示为任何内容,或者触发错误图像,而是显示为image0001.png
确实存在。
我不知道如何解决这个问题,因为我真的不知道发生了什么。
我使用以下代码从数据库中获取结果并生成包含项目详细信息的表。
<?php while ($row = $retval->fetch_array()) { ?>
<tr>
<td>
<a href="/path/to/item/<?= $row['id']; ?>.php"><?= $row['id']; ?></a>
</td>
<td>{$row['name']}</td>
<td>
<a href="/path/to/item/<?= $row['id']; ?>.php">
<img src="/images/<?= $row['id']; ?>.jpg" onerror="this.src='/images/error.png';">
</a>
</td>
<td><?= $row['description']; ?></td>
</tr>
<?php } ?>
答案 0 :(得分:1)
听起来你的服务器上已经启用了mod_speling。
核心apache服务器有时无法提供对文档的请求,因为请求拼写错误或资本错误。该模块通过尝试查找匹配的文档来解决此问题,即使在所有其他模块放弃之后也是如此。
您可以尝试在.htaccess文件中禁用此apache模块:
CheckSpelling Off
答案 1 :(得分:0)
显而易见的另一种选择是使用file_exists
,删除JavaScript的依赖并确保图像显示(如果存在)或错误图像(如果不存在)。
<?php while ($row = $retval->fetch_array()) { ?>
<tr>
<td>
<a href='/path/to/item/<?= $row['id']; ?>.php'><?= $row['id']; ?></a>
</td>
<td>{$row['name']}</td>
<td>
<a href='/path/to/item/<?= $row['id']; ?>.php'>
<?php if (file_exists("/images/{$row['id']}.jpg")) { ?>
<img src="/images/<?= $row['id']; ?>.jpg" />
<?php } else { ?>
<img src="/images/error.png" />
<?php } ?>
</a>
</td>
<td><?= $row['description']; ?></td>
</tr>
<?php } ?>