Roberto Crossa Roberto Crossa - 10 months ago 49
PHP Question

Issues with relative paths in PHP includes

Before going on, I'd just like to say that I've tried pretty much everything, and I read a lot of questions here at Stackoverflow, but none of the solutions worked.

So here is the deal, I have a file ("empresaConf.php") that contains this code:

include 'navigator2.php';

<div class="contTudo">

<div id="bgCorpo">

<div class="empresaConf">

include 'sqEsqInc.php'



include '../rodape.php';



As you can see, it is including "rodape.php". The problem is that in "rodape.php", I have an image that is in another folder, so when I include "rodape.php" in "empresaConf.php", the image path should change, since PHP include consider the file path to be the one that you're including other files, making rodape.php's image broken when I access "empresaConf". I want something to fix that so I have the image no matter what file I'm in. Here is the "rodape.php" code:

<div class="empresa">

/* below in the img src tag you can see what I'm talking about */
<div class="redeSocial">
<a href="">
<img src="imgs/icons/iconFace42.png" class="iconeFace">

</div> <!-- Fim div .empresa -->

The folder structure is: "rodape.php" is in the root directory and "empresaConf.php" is inside a folder named "Empresa" (I would post a img but I can't).

Thanks in advance.

Sorry, I forgot a crucial detail, I'm also including "rodape.php" in other files such as "index.php". So if I change the img src to correspond "empresaConf.php", then in the "index.php" the image will be broken, since it's in the root directory, same as "rodape.php".

Answer Source

If it's an image accessible from the public side of things, the image can probably be accessed by the directory root. So if your index.php is in /index.php where / is the web document root and your image is in /imgs/icons/iconFace42.png you can access the image by

    <img src="/imgs/icons/iconFace42.png" class="iconeFace">

Note the leading slash in the src attribute.