PHP file outside the doc root requires files outside and inside the document root

I have a class library, all interconnected.

Some files are inside the root of the document, and some are outside using functions <Directory> and Aliasin httpd.conf

Assuming I have 3 files:

webroot.php (Inside the document root)
alias_directory.php (Inside a folder outside the doc root)
alias_directory2.php (Inside a **different** folder outside the doc root)

If alias_directory2.php is needed both webroot.php and alias_directory.php, this does not work. (Remember that alias_directory.php and alias_directory2.php are not in the same place)

require_once $_SERVER['DOCUMENT_ROOT'].'/webroot.php'; //(ok)
require_once $_SERVER['DOCUMENT_ROOT'].'/alias_directory.php'; //(not ok)

This does not work because alias_directory.php is not in the root of the doc.

Similarly

require_once $_SERVER['DOCUMENT_ROOT'].'/webroot.php'; //(ok)
require_once dirname(__FILE__).'/alias_directory.php'; //(not ok)

The problem is that it dirname(__FILE__)will return the path for alias_directory2.php and not alias_directory.php.

It works:

require_once $_SERVER['DOCUMENT_ROOT'].'/webroot.php'; //(ok)
require_once '/full/path/to/directory/alias_directory.php'; //(ok)

But this is very unpleasant and is a nightmare for maintenance if I decided to move my library to another location.

, , Alias.

+3
2

PHP alias_directory.php. alias_directory.php . , alias_directory2.php DOCUMENT_ROOT

:

If your file locations look something like this:
/var/www/webroot.php
/var/otherdir1/alias_directory.php
/var/otherdir2/alias_directory2.php

From alias_directory2.php you can now include alias_directory.php by either:
require_once('/var/otherdir1/alias_directory.php');
or
require_once(dirname(__FILE__).'/../otherdir1/alias_directory.php');
or
require_once($_SERVER['DOCUMENT_ROOT'].'/../otherdir1/alias_directory.php');

, alias_directory.php - alias_directory2.php DOCUMENT_ROOT

include_path require_once ('alias_directory.php'); .

0

dirname . , , , ,

require_once dirname( dirname(__FILE__) ) . '/other_dir/alias_directory.php';
0

Source: https://habr.com/ru/post/1749758/


All Articles