How to get absolute path for my site in php?

I have an issue with finding paths with includes in my site. I use the method i know but coud not get it right: Here is how my site is composed.

1- Main foldername is:kbaboutik: Inside we have:
[php]images
css
includes
index.php

subfoldername:Man-clothes
index.php[/php]

So, the issue have is that I have tried to include header.html.php from the includes folder into man-clothes index.php with this method:
[php]

<?php $pathroute = $_SERVER['DOCUMENT_ROOT']; include_once $pathroute."kbaboutik/includes/header.inc.php"; ?>[/php]

But it does not work the way expected; the html element of the header.inc.php apppears unstyled and the images also do not not appear.

Can somebody help me out?

Thanks

If your includes folder is in the same folder as the page calling it just do the following

include_once ("./includes/header.inc.php")

The DOCUMENT_ROOT constant gives the base path, so something like var/home/username

What I have done in a number of projects is to create a constant for the BASE_PATH.

[php]define ( ‘BASE_PATH’, $_SERVER[‘DOCUMENT_ROOT’] . ‘/project_name’);[/php]

Then to use it,

[php]include BASE_PATH . ‘/dir/to/file.php’;[/php]

It also wise to sanitize it -
[php]$document_root = filter_input(INPUT_SERVER, “DOCUMENT_ROOT”, FILTER_SANITIZE_URL);[/php]

Actually [member=72272]astonecipher[/member], if the full path is actually needed we would simply do like

[php]$path = realpath(dirname(FILE));[/php]

then using it in the op’s case

[php]include “$path/includes/file.php”[/php]

Now we know, or should now Windows uses back slashes for directory separators, linux forward slashes. So our cross platform solution is the following:

[php]$path = realpath(dirname(FILE)).DIRECTORY_SEPARATOR;

include ("{$path}includes".DIRECTORY_SEPARATOR.“file.php”);[/php]

I am not sure if it the case anymore, but at least at one time there was/is a cross-platform issue with $_SERVER[‘DOCUMENT_ROOT’] that takes a mess of code to be able to use it on Windows and Linux.

True, you could aslo do this,
[php]$path = dir; [/php]

And Using $_SERVER[‘DOCUMENT_ROOT’] on a windows box reverses the directory separator / to .

As with most things there are a ton of ways to accomplish the task.

Back to my Java studies!

Nice job [member=72272]astonecipher[/member]! You one up’d both our previous answers by a factor of a thousand. I have some code to update. Thanks Brutha!

  • Yeah, I just checked, the doc root thing was about the slashes.

[member=57087]Strider64[/member], document root is not user supplied data so why would you need to sanitize it?

Thanks to you all…it is the same issue i am having the header appears unstyled and no images appears. I am

putting the full site structure and the header content… i am working on local server before going live…

in the main index.php everything works fine because the link is relative: so the

[php]include_once ‘includes/header.inc.php’; [/php]

but when i want to include the header.inc.php into the index.php located in the subfolder ManClothes, Things

go wrong the header appears into simple html totally unstyle and the logo images do not appear as well…

So , that is an absolute path situation so i use this technique in index.php located in the subfolder ManClothes like this:

[php]ManClothes subfolder:index.php

<?php define('ROOT', $_SERVER['DOCUMENT_ROOT']); if (!include_once(ROOT."kbashopping/includes/nav.inc.php")) { echo "Fail to open"; } ?>

[/php]
only the raw html appears and totally unstyled.

This is the full site structure
[php]site foldername ->kbaboutik

inside kbaboutik folder

main : index.php

And there are these subfolder

images
-all site images and logo
css
-main.css
includes
-header.inc.php
-footer.inc.php
ManClothes
-index.php
[/php]

This is how the header.inc.php looks like:

[php]

Achat en ligne | Achat de chaussures, chaussures grandes tailles de qualité à pétit prix en Côte d'Ivoire | kbaboutik.com .topNav i a { color:#f09918; } .fa { position: absolute; display: block; }
</head>

Bienvenue Visiteur ! Connectez - vous ou Inscrivez - vous| Vendre sur kbashopping | Aide

Les meilleurs deals | Promotion | Mon KbaCompte    Kba_panier

<!--debut TopHeaderBar-->
<div class="TopHeaderBar">             
 <a href="#" class="logo imgBorder">
<img src="images/kbaLogo1.png" alt="Kbashop_logo"/>
</a>    
    <div class="topHeaderIcons imgBorder">
<ul><li><img src="images/kba_payment1.png" width="130" height="55" alt="payment_icon" /></li><li><img src="images/kba_livre.png" alt="livraison_icon" width="130" height="55" /></li><li><img src="images/contact_kba.png" alt="kba_contact"  width="130" height="55" /></li>
    </ul></div>  
    
  <div id="search">
      
      <div class="searchBtn">



  </div><!--end#TopHeaderBar -->
</header> <!--end#headerwrapper-->  [/php]

I am really feeling desperate with this situation. I have tried to explain the best i could…hope you guys will see the issue and help me better

Thanks

I gave you the simplest answer in my first post. In the case of man clothes directory one small change, add one more dot. Your other problem is you RE-defined root. You only define it once in a main config file that is included everywhere. That is probably the biggest problem.

include_once ("../includes/header.inc.php")

  • You should always use lower case naming.

Your going to end up with a very messy file structure doing what your doing. Download the PDO bumpstart Database from my signature link. Way better structure.

This is a quote from my IDE -
Do Not Access Superglobal $SERVER array directly.
use some filtering instead (eg. filter_input(), conditions with is
*() functions, etc…);

Let me guess, NetBeans right?

Sponsor our Newsletter | Privacy Policy | Terms of Service