Link to home
Start Free TrialLog in
Avatar of altariamx2003
altariamx2003Flag for Mexico

asked on

¿It is safe to use full paths in php?

I have this problem

Im try to make a header with php

In this header I have for example code like this:


echo '<img  src="imagenes/example.jpg" width="100" height="100">';

Open in new window


When I use my header from the main page of my site (http://mysite.com/index.php) my header works without problems.

But if Im in a directory inside of my site my header cannot find the image

I was thinking to use absolute paths in my header for example:
echo '<img src="/home/myserver/public_html/imagenes/example.jpg" width="100" height="100">';

Open in new window


¿But it is safe to use full paths?
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

Sure its safe, just that it is not very flexible.
If you do that and you have to move your site location you may have to go through the site re-pointing all your links.
Avatar of altariamx2003

ASKER

ok

you are right it is not flexible

I made this to make it flexible

$one=explode('/', getcwd()); 
$two=explode('/', $_SERVER['DOCUMENT_ROOT']); 
$dif = count($one)-count($two);
for($x=1;$x<=$dif;$x=$x+1)
{ $string= $string.'../'; }
echo '<img src="'.$cadena.'imagenes/example.jpg" width="100" height="100">';

Open in new window


If i use that the site will be slower???
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not enough to make a difference though I am not really sure what you are trying to do with it.
You don't seem to be using $string anywhere and $cadena just shows up with out being initialized in that snippet of code.
sorry halo it was my error this is the correct snippet

$one=explode('/', getcwd()); 
$two=explode('/', $_SERVER['DOCUMENT_ROOT']); 
$dif = count($one)-count($two);
for($x=1;$x<=$dif;$x=$x+1)
{ $mystring= $mystring.'../'; }
echo '<img src="'.$mystring.'imagenes/example.jpg" width="100" height="100">';

Open in new window


the variable mystring, help me to ad the correct "../" to the path of the image
It works

So simply solution