4

My current document root is this (via $_SERVER['DOCUMENT_ROOT']):

/var/www/html/clients/app/folder

I need to generate one folder up:

/var/www/html/clients/app

How would I go about doing this?

I had asked this in the past: Dynamically finding paths, is there a better way?

However, I have this scenario which doesn't work:

  • Executed script is located here: root/f1/f2/f3/f4/f5/file.php.
  • This script includes another script located here: root/f6/file2.php

In file2.php, I needed the following code for this to work:

$base_path = dirname(realpath("../../../../do_not_remove.txt"));

When in theory, based on its location, it should have been this:

$base_path = dirname(realpath("../do_not_remove.txt"));

In practice, there would be a global available where this data could be passed. However, in this inherited project, there isn't thus I'm reusing this where I need it.

Update #1

Based on the answers, this seems to work great: realpath($_SERVER['DOCUMENT_ROOT']."/../../");

4

3 回答 3

5

well you could have - $_SERVER['DOCUMENT_ROOT'] ."/../" - even though it doesn't look too pretty

OR a slightly more propper way might be - dirname( $_SERVER['DOCUMENT_ROOT'] ) - think this should work

于 2012-10-18T19:46:43.747 回答
3
$path = $_SERVER['DOCUMENT_ROOT']."/../";
于 2012-10-18T19:46:52.623 回答
2

Using explode(), array_pop() and implode() :

<?php

//$path = $_SERVER['DOCUMENT_ROOT'];
$path = "/var/www/html/clients/app/folder";
$tmp = explode("/", $path);
array_pop($tmp);
$path_up = implode('/', $tmp);
echo $path_up; //output: /var/www/html/clients/app

?>
于 2012-10-18T19:52:13.240 回答