在文件夹路径外定义webroot

I have folder structure like this:

  public_html
     /index.php   
  outside
     /other.php

I want in public_html/index.php include file, wich is place in outside folder. So, I want define outside folder path in public_html/index.php file. I make this so:

index.php

 $doc_root_upper = preg_replace("#/[^/]+$#", "", $_SERVER["DOCUMENT_ROOT"]);
 $outside_folder = $doc_root_upper."/outside";
 include outside_folder.'/other.php';

This works, but there is better practice for define webroot outside folder path?

My personal preference is to start my init.php file (included by php.ini's auto_prepend_file directive) with chdir($_SERVER['DOCUMENT_ROOT']).

This means that all of my includes and other file functions are done from the document root.

In your case, this means you can then do include ../outside/other.php reliably.

Use a relative path like this:

Include("../outside/other.php");

../ means up one directory.

I like to use a chdir() to get the explicit paths and then add them to the include path. I tend to have a file called init.php that contains the following:

$path = get_include_path(); // get the current path
$oldcwd = getcwd(); // get the current path

// change to outside of the document root
chdir($_SERVER['DOCUMENT_ROOT'] . "/.."); 
$newcwd = getcwd(); // get the new working directory

// build the new path. Using hte _SEPARATOR constants ensures 
// that it works on all platforms.
$newpath = $path . PATH_SEPARATOR . $newcwd . DIRECTORY_SEPARATOR . "outside"; 

// Set the new path
set_include_path($newpath);

chdir($oldcwd); // change back to the original working directory