When I use require_once I get the following error:
"PHP Fatal error: require_once(): Failed to opening required '/Application/Rules/required_file.class.php'"
My directory structure is:
"file" is where I use require_once
:
require_once('/Application/Rules/required_file.class.php');
Why the error? Can anyone help me with the solution?
If I got you right, you are calling the
require_once('/Application/Rules/required_file.class.php');
within the file
? That does not work because require_once
always relates to the current folder, as long as you do not use absolute paths.
Instead you need to call
require_once('../../Application/Rules/required_file.class.php');
because the file you want to include is not in the same folder as your file
.
../
goes back one folder in the hierarchy.
../../
therefore goes back from /Users
to /Public
and then to /Project
, form where you then can go to /Application
.
I think this article might explain the difference between relative and absolute paths quite well.
The relative path points to a file or directory in relation to where the present file is located.
The absolute path is the "full path" from the webserver point of view. It is the path that contains the document root. For example /var/www/mydomain.com/.
/Application/Rules/...
is unix absolute path
either use relative path ../../Application/Rules/...
or proper absolute path
/var/www/public_html/Project/Application/Rules/...
(unix, example)
or
C:/wamp/projects/Project/Application/Rules/...
(windows, example)
use DIRECTORY_SEPARATOR will work for windows and unix
define('DS', DIRECTORY_SEPARATOR);
require_once('Application' .DS. 'Rules' .DS. 'required_file.class.php');