I have folder structure like this:
public_html
/index.php
outside
/other.php
I want in public_html/index.php include file, which 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.phpfile (included by php.ini’sauto_prepend_filedirective) withchdir($_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.phpreliably.