-2

Possible Duplicate:
Getting relative path from absolute path in PHP

When including PHP-Files I often use absolute paths like

/etc/..../index.php

As I don't own the server those files may not be found if the admin changes position of the files.

But trying to find the right path using

..

each time when changing files is a hassle.

Is there a function or script I can pass my

/etc/..../index.php

to for it to return the relative path I can use with include?

Community
  • 1
  • 1
Zurechtweiser
  • 1,165
  • 2
  • 16
  • 29
  • Please perform a search before posting. Related posts also appear whilst you're writing your question. Several of them are duplicates of yours. – Lightness Races in Orbit Jul 07 '11 at 09:06
  • 1
    At any rate, if the admin changes the position of the files, a relative path will not work. A relative path is only really useful if you want to ensure that all references are maintained when the structure is maintained but the path has changed, i.e. When moving from a test server to live. – Sam Parmenter Jul 07 '11 at 09:23

3 Answers3

1

dirname(__FILE__) returns the current directory name dirname(dirname(__FILE__)) takes you one level up etc. its the best way to include files

Ryan
  • 1,119
  • 7
  • 10
  • For readability I suggest using `dirname(__FILE__) . '/../'` instead of subsequent `dirname()`-calls. Because `.` and `..` are folders, the resulting path remains absolute. Also PHP5.3 introduces `__DIR__` as replacement for `dirname(__FILE__)`. – KingCrunch Jul 07 '11 at 09:40
1
__DIR__.'/../../../index.php'

DIR returns directory of the file it was called from.

Inoryy
  • 8,365
  • 2
  • 39
  • 40
0

Tomalak Geret'kal is right, there is an algorithm for you here: Getting relative path from absolute path in PHP

As an alternative function you can use one posted by http://iubito.free.fr in PHP manual in 2003:

/**
 * Return the relative path between two paths / Retourne le chemin relatif entre 2 chemins
 *
 * If $path2 is empty, get the current directory (getcwd).
 * @return string
 */
function relativePath($path1, $path2='') {
if ($path2 == '') {
    $path2 = $path1;
    $path1 = getcwd();
}

//Remove starting, ending, and double / in paths
$path1 = trim($path1,'/');
$path2 = trim($path2,'/');
while (substr_count($path1, '//')) $path1 = str_replace('//', '/', $path1);
while (substr_count($path2, '//')) $path2 = str_replace('//', '/', $path2);

//create arrays
$arr1 = explode('/', $path1);
if ($arr1 == array('')) $arr1 = array();
$arr2 = explode('/', $path2);
if ($arr2 == array('')) $arr2 = array();
$size1 = count($arr1);
$size2 = count($arr2);

//now the hard part :-p
$path='';
for($i=0; $i<min($size1,$size2); $i++)
{
    if ($arr1[$i] == $arr2[$i]) continue;
    else $path = '../'.$path.$arr2[$i].'/';
}
if ($size1 > $size2)
    for ($i = $size2; $i < $size1; $i++)
        $path = '../'.$path;
else if ($size2 > $size1)
    for ($i = $size1; $i < $size2; $i++)
        $path .= $arr2[$i].'/';

return $path;
}
Community
  • 1
  • 1
WASD42
  • 2,352
  • 6
  • 27
  • 41