问题
I have been facing an issue with the PHP includes. Based on the readings from internet, I have been using relative path to have the flexibility around.
As long as I refer to a path directly in each file, like
require_once '../includes/connection.php'; // it works fine.
The issue starts when I refer to this path with a global constant
require_once getDocumentRoot() . '/includes/connection.php',
as it says the below error.
Warning: require_once(/phpPractices/myApp/includes/connection.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/phpPractices/myApp/dir1/list.php on line 19".
I had been reading so many threads in SO and following the advices like
- Define a constant as DOCUMENT_ROOT with the base director of the file. (Like the one what it says here in SO -> https://stackoverflow.com/a/22912261/1001242)
- Also attempted with various options like DIR, basedir(FILE) etc.,
- Added my application directory (/phpPractices/myApp) to the include_path via set_include_path() method (as mentioned in PHP, How to set include path and http://php.net/manual/en/function.set-include-path.php)
My function getDocumentRoot() is defined as follows.
function getDocumentRoot()
{
$loc_pos_2 = strpos($_SERVER['REQUEST_URI'], "/", 1);
$baseDir = substr($_SERVER['REQUEST_URI'], 0, $loc_pos_2);
$baseDir = $baseDir . "/myApp";
return $baseDir;
}
Note: I could see that the file exists physically. However, file_exists() method returns false, which I don't understand why. Possibly it could have been a cause of concern?
FYI, my directory structure is as below.
htdocs
phpPractices
myApp --> Web App Root
includes
connection.php
inc
header.php
footer.php
global.php -- contains getDocumentRoot() method and included in header.php
index.php
dir1 --> my module specific dir
list.php --> which needs 'connection.php'
My list.php contains the below
<?php
include_once '../inc/header.php'; // it works fine as I do relative to the path
//require_once '../includes/connection.php'; //works fine
require_once getDocumentRoot() . '/includes/connection.php'; // FAILS! :(
?>
But nothing helps! Any suggestion to get rid of this issue will be much appreciated.
Thanks in advance!
SOLUTION
Thanks to all the people whoever had replied. I got a new workaround that kinda helps me :)
1. For all the menu items with links:
I use my getDocumentRoot() method which provides me -
'/phpPractices/myApp/' --> which is relative to my Web App.
2. For all the include, require functions :
I defined a different constant as below & as per this link:
https://stackoverflow.com/a/1488293/1001242
define('APP_BASE_PATH', $_SERVER['DOCUMENT_ROOT'] . '/myApp');
which returns me the complete absolute path which works fine for file inclusion.
I had been meddling with these both. For the file inclusion, the getDocumentRoot()'s output of "/phpPractices/myApp" was failing as it was attempting to locate a directory named 'phpPractices' in the root ("/phpPractices/"), which was the root cause. Hence, I had split these into two and now works fine.
Thanks everyone. Much appreciated.
回答1:
Update your getDocumentRoot() function to return absolute path
function getDocumentRoot()
{
return dirname(__FILE__);
}
or use dirname(__FILE__)
in your require
来源:https://stackoverflow.com/questions/27586445/issue-with-php-include-with-global-path