3

I have a library of classes, all interrelated.

Some files are inside the document root and some are outside using the <Directory> and Alias features in 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 needs both webroot.php and alias_directory.php, This does not work. (Remember alias_directory.php and alias_directory2.php are not in the same locations)

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 doc root.

Similarly

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

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

This works:

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

But is very nasty and is a maintenance nightmare if I decide to move my library to another location.

How do I solve this problem, is seems that I need a way to resolve an Alias folder properly.

2
  • why everyone tend to use dirname(__FILE__) in the file access context, no matter if it suitable or not? Looks like everyone take this another way to write a relative path as a magic spell to solve any filesystem problem Jun 12, 2010 at 18:32
  • No <Directory> nor Alias can affect filesystem calls. Jun 12, 2010 at 18:58

2 Answers 2

0

PHP will not be able to resolve the location of your alias_directory.php file. You will need to supply the location of the alias_directory.php file yourself. Either as an absolute path, or relative to alias_directory2.php or the DOCUMENT_ROOT

Example:

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');

To use relative paths, alias_directory.php should be located somewhere close to alias_directory2.php or DOCUMENT_ROOT

Another solution would be to add the folders in question to the include_path, and use require_once('alias_directory.php'); without path specification.

2
  • what's the use of dirname(__FILE__) here? what if it will be called from /var/www/themes/cyan/template.php? Jun 12, 2010 at 18:34
  • As I understood the question, alias_directory.php was to be included from alias_directory2.php. If the two files are known to be close to each other (as in sibling directories), dirname(FILE) will provide the closest starting point for a relative path. Jun 12, 2010 at 18:44
0

Use dirname twice. Once to get the directory of the current file, again to get the parent of that directory, then step back down into the other directory;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.