4

I am trying to require my "library" files from php files in different folders, but it gives errors when trying to access them from a subfolder. For example I have such a directory:

+ home    
- file1.php

 ++ subfolder
  - file2.php

 ++ libraries
  - code_generator.php
  - database_library.php

code_generator.php also depends on the other library:

(LINE 25) require_once(realpath("./libraries/database_library.php"));
//this works fine when called from file1.php, but not from file2.php

I try to call code_generator from ./subfolder/file2.php like:

chmod("../libraries/codegenerator.php", 0777);  // for the error at the end of this question
require_once("../libraries/code_generator.php");

When I run file2.php, it yields 2 errors:

**Warning**: require_once(C:\xampp\htdocs\home\subfolder): failed to open stream: Permission denied in C:\xampp\htdocs\home\libraries\code_generator.php on line 25

**Fatal error**: require_once(): Failed opening required '' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\home\libraries\code_generator.php on line 25

*line 25 is require_once(realpath("./libraries/database_library.php"));

3
  • 3
    I guess that if you do not have permissions to open this file, neither you will have to chmod it.
    – fedorqui
    Feb 8, 2013 at 15:56
  • @AD7six that's what I'm suspecting. the realpath() function returns correctly from file1.php, but it returns empty in file2.php. I guess I'm using that wrong. But what's the right way?
    – marvin
    Feb 8, 2013 at 15:59
  • @fedorqui I noticed this in PHP manual for realpath() : "The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE." So how will I get the "permissions"?
    – marvin
    Feb 8, 2013 at 16:02

2 Answers 2

4

You can use the PHP super global variable $_SERVER['DOCUMENT_ROOT'] to get the full path to your server document root, eg. /var/www/website/public.

The code could look like this:

require_once $_SERVER['DOCUMENT_ROOT'] . '/libraries/database_library.php';

This way you do not need to set the document root path manually; PHP will always pick it up for you and make it available in $_SERVER['DOCUMENT_ROOT'] in any PHP file that is within your server document root.

3

The file does not exist.

The error message reads:

Warning: require_once(C:\xampp\htdocs\home\subfolder): failed to open stream:

And:

Failed opening required ''

Error messages never lie (well, almost never), therefore you are calling:

require_once('');

Realpath returns false for missing files

Realpath will return false if you pass it a path to a file that doesn't exist:

$return = realpath('this/doesnt/exist.foo'); // $return is false

Therefore, the argument passed to realpath doesn't refer to a file, which is the reason the path doesn't work.

Don't over use realpath

If you remove the use of realpath, you'll find that the error message is probably obvious. At a guess you're trying to include C:\xampp\htdocs\home\subfolder\libraries\database_library.php - because there is a missing . in this:

line 25 is require_once(realpath("./libraries/database_library.php"));

If instead you just define a root path:

define('ROOT', 'C:\xampp\htdocs\home\\');

Then you can use this constant with absolute confidence that these sort of problems wont occur:

require_once(ROOT . "libraries/database_library.php");

And if you do get the path wrong, the error message will make it obvious what the problem is.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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