使用 PHP 到 link 到 SRC 时图像不显示

Image not showing when using PHP to link to SRC

尝试将 php 到 link 用于与我当前所在的文件不同的文件夹中的图像,但图像未显示。

这是路径文件:

<?php
if (!defined("ROOT_PATH")) define ("ROOT_PATH", realpath(dirname(__FILE__)));
if (!defined("BASE_URL")) define ("BASE_URL", "http://localhost/stnresp");
?>

这是我目前使用的代码:

<div class="logo-image">
  <a href="<?php echo BASE_URL . '/index.php'; ?>"><img class="header-logo" src="<?php echo ROOT_PATH . '/assets/images/stn-logo-cropped.png'; ?>" alt=""></a>
</div>

我不明白为什么根路径不把 link 带到根目录,然后通过图像?我错过了什么?

谢谢

realpath定义文件的物理路径,而不是图像的src属性需要url路径。

在你的情况下你必须使用 BASE_URL 而不是 ROOT_PATH:

<div class="logo-image">
    <a href="<?php echo BASE_URL . '/index.php'; ?>"><img class="header-logo" src="<?php echo BASE_URL . '/assets/images/stn-logo-cropped.png'; ?>" alt=""></a>
</div>