Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting path from document root in PHP

Tags:

php

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']."/../../");

like image 968
TechFanDan Avatar asked Dec 26 '22 15:12

TechFanDan


2 Answers

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

like image 51
kalpaitch Avatar answered Jan 13 '23 00:01

kalpaitch


$path = $_SERVER['DOCUMENT_ROOT']."/../";
like image 45
Ray Avatar answered Jan 13 '23 01:01

Ray