Reputation: 4180
I have a website is located in /proj/hcr/
(the Apache DocumentRoot is /proj). I am trying to include a file using an absolute path, but it says that the file does not exist even though it does. The line of code is as follows: include_once '/hcr/spaces-api/spaces.php';
Here is the directory structure for the file I am trying to include
Upvotes: 0
Views: 167
Reputation: 63
Your question title says that include with absolute path doesn't work in PHP, but in your question I read:
I am trying to include a file using a relative path, but it says that the file does not exist even though it does. The line of code is as follows:
include_once '/hcr/spaces-api/spaces.php';
I am assuming you are trying to include file with '/hcr/spaces-api/spaces.php'
(which is supposed to be an absolute path).
Absolute path doesn't start from root directory by default,so '/hcr/spaces-api/spaces.php'
is not an absolute path since it lacks the path to the root directory. You need to provide the full path to the file on the machine. For example , absolute path on windows machine (using xampp as a web server,installed on C) would be:
C:/xampp/htdocs/hcr/spaces-api/spaces.php
You should use the following code:
include_once $_SERVER["DOCUMENT_ROOT"].'/hcr/spaces-api/spaces.php';
So , $_SERVER["DOCUMENT_ROOT"]
will give you path to the root directory and you can then concatenate it with path to file in root directory.
Upvotes: 1