【问题标题】:Image not showing when using PHP to link to SRC使用 PHP 链接到 SRC 时图像未显示
【发布时间】:2020-05-17 16:59:13
【问题描述】:

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

这是路径文件:

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

我不明白为什么根路径没有将链接指向根目录,然后再指向图像?我错过了什么?

谢谢

【问题讨论】:

    标签: php html image filepath


    【解决方案1】:

    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>
    

    【讨论】:

      猜你喜欢
      • 2020-06-25
      • 2019-06-13
      • 2023-02-05
      • 2020-04-10
      • 2021-09-28
      • 2011-03-10
      • 2022-11-02
      • 2018-02-15
      • 1970-01-01
      相关资源
      最近更新 更多