We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

frankyfish on 04/13/07


Tagged


Versions (?)


Who likes this?

6 people have marked this snippet as a favorite

basicmagic
vali29
hudge
heinz1959
benrasmusen
jonhenshaw


Get relative path to root


Published in: PHP 


  1. $path = "";
  2. for($i=count(array_pop(explode("/",dirname($_SERVER['PHP_SELF'])))); $i>0; $i--) $path .= "../";
  3.  
  4. echo $path;

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: phatrick on May 21, 2008

Think what you meant was $i>=0; which works not $i>0;

$path = "";

for($i=count(arraypop(explode("/",dirname($SERVER['PHP_SELF'])))); $i>=0; $i--) $path .= "../";

echo $path;

Posted By: skcsknathan001 on June 20, 2008

Thank you very much.

I was not able to get DOCUMENT_ROOT or any other server variables. All was returning empty.

I used this and it's working. I modified

$path = ""; for($i=count(explode("/",dirname($SERVER['PHPSELF']))); $i>1; $i--) $path .= "../"; echo $path;

this works from root folder itself and any level of subfolders

Thanks again

You need to login to post a comment.