Home > OS >  image path not working when accessing php include file from two different folders
image path not working when accessing php include file from two different folders

Time:08-25

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  src="img/logo.png" alt="" title="" />
</div>

<nav id="nav-menu-container">
 <ul >
  <li ><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.

CodePudding user response:

Use absolute path for the image https://your.subdomain.com/main-website/img/logo.png. I hope this helps.

CodePudding user response:

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/.

  •  Tags:  
  • php
  • Related