简体   繁体   English

访问 php 时图像路径不起作用包括来自两个不同文件夹的文件

[英]image path not working when accessing php include file from two different folders

I have included a 'header' file in files which are in root folder and the header file is in 'inc' folder.我在根文件夹中的文件中包含了一个“头”文件,header 文件在“inc”文件夹中。 header.php is completely working when accessing from subdomain/main-website/index.php file.从子域/主网站/index.php 文件访问时,header.php 完全正常工作。 But when accessing from subdomain/index.php, only the logo is not showing due to the relative path.但是当从 subdomain/index.php 访问时,由于相对路径,只有 logo 没有显示。

Folder Structure: subdomain/main-website文件夹结构:子域/主网站

  • css css
  • img图像
  • inc (header.php inside inc folder) inc(在inc文件夹中的header.php)
  • index.php ( <?php include_once 'inc/header.php'?> ) index.php ( <?php include_once 'inc/header.php'?> )
  • about.php ( <?php include_once 'inc/header.php'?> ) about.php ( <?php include_once 'inc/header.php'?> )

subdomain子域

  • main-website主网站
  • index.php ( <?php include_once 'main-website/inc/header.php'?> ) index.php ( <?php include_once 'main-website/inc/header.php'?> )

header.php header.php

<div>
 <img class="logoImg" src="img/logo.png" alt="" title="" />
</div>

<nav id="nav-menu-container">
 <ul class="nav-menu">
  <li class="menu-active"><a href="index.php">Home</a></li>
  <li><a href="about-us.php">About Us</a></li>
 </ul>
</nav>

So what should be done or what is the best approach for this.那么应该做什么或最好的方法是什么。

Use absolute path for the image https://your.subdomain.com/main-website/img/logo.png .使用图像https://your.subdomain.com/main-website/img/logo.png的绝对路径。 I hope this helps.我希望这有帮助。

You can always anchor yourself on the root folder by using ../path-to-sub-folder/ .您始终可以使用../path-to-sub-folder/将自己锚定在文件夹上。 Accordingly, you must then give the folder names starting from this root - base excluded.因此,您必须提供从此根目录开始的文件夹名称 - 排除基础。 This should work as long as base directory carries all your sub-domains.只要基本目录包含您的所有子域,这应该可以工作。 But as a last resort, and NOT for public-facing links , you can refer to absolute file reference like /var/www/html/base_folder/sub_folder/ .但作为最后的手段,而不是面向公众的链接,您可以参考绝对文件参考,例如/var/www/html/base_folder/sub_folder/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM