简体   繁体   中英

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.php is completely working when accessing from subdomain/main-website/index.php file. But when accessing from subdomain/index.php, only the logo is not showing due to the relative path.

Folder Structure: subdomain/main-website

  • css
  • img
  • inc (header.php inside inc folder)
  • index.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'?> )

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 . I hope this helps.

You can always anchor yourself on the root folder by using ../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/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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