includes in php
I include many pages inside each other in my project, I face prob开发者_如何学编程lem of incorrect paths inside the include, is there any way to include the file from the root, I try to make like this
require_once("$_SERVER['DOCUMENT_ROOT']/crm/includes/opendb.php");
require_once("./includes/opendb.php");
require_once("../includes/opendb.php");
require_once("includes/opendb.php");
and all makes error in includeing the file, any suggestion plz?
Edit
I have 2 folders, controllers and includes, inside the controller I call opendb.php from includes folder, I make as demonstrated in an answer below
set_include_path('../includes/');
include 'opendb.php';
and
set_include_path('includes/');
include 'opendb.php';
but the same error
Warning: include(opendb.php) [function.include]: failed to open stream: No such file or directory in C:\AppServ\www\crm\controllers\Customer.controller.php on line
"$_SERVER['DOCUMENT_ROOT']/crm/includes/opendb.php"
is incorrect: you can't use apostrophes around array keys inside double quotes. Try one of these:
require_once( $_SERVER['DOCUMENT_ROOT']."/crm/includes/opendb.php" );
require_once( "$_SERVER[DOCUMENT_ROOT]/crm/includes/opendb.php" );
require_once( "{$_SERVER['DOCUMENT_ROOT']}/crm/includes/opendb.php" );
There are many possible solutions (from include_path to class autoloading) but I think that setting absolute paths is what tends to work better in practice: it's simple, intuitive and can't be easily ruined by sysadmins and third-party libraries.
You don't say why $_SERVER['DOCUMENT_ROOT']
does not work for you but I've found that it only works 99% of the times: some hosting providers do not set it or set it to a wrong value. Also, it's possible that the application is not directly in the document root. To overcome this, I normally write a settings.php
file that's loaded manually on top of all scripts:
// One of...
require(dirname(__FILE__) . '/conf/settings.php');
require(dirname(__FILE__) . '/../conf/settings.php');
require(dirname(__FILE__) . '/../../conf/settings.php');
In settings.php
I create two PHP constants:
define('FS_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/my-app/');
define('WEB_ROOT', '/my-app/');
I prefer constants over variables because they're available in all scopes (and they are constant values anyway). When setting their values, you can use as many logic as you want (for instance, you can calculate the my-app
part rather than hard-coding it) but that's the general idea:
require_once(FS_ROOT . 'utils/email.php');
echo '<script type="text/javascript" src="' . WEB_ROOT . 'js/validate.js"></script>';
You can use set_include_path() to change the include path.
Let's say you have this document structure:
php/include.php
php/include2.php
file.php
And php/include.php
has this code:
include 'include2.php';
If you were to include php/include.php
in file.php
like this:
include 'php/include.php';
It will cause an error on include2.php
because you are not in the php/
folder.
But if you do this:
set_include_path('php/');
include 'include.php';
It will work.
I'm guess that file includes/opendb.php
is configuration for connection to the database?
If I'm right then it will be better for you to use OOP and MVC to separate view and logic and try to look for dependecy injection container (DIC) for configuring services, etc.
After that you will be able to use autoloaders for PHP classes and make changes in templates (include another templates, etc). This will prevent you from annoying require_once("includes/opendb.php");
.
If $_SERVER[ 'DOCUMENT_ROOT' ]
doesn't work (it doesn't on some configurations like PHP+IIS) use one of the tricks below whichever works:
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/',
substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])));
or
$_SERVER['DOCUMENT_ROOT'] = str_replace( '\\', '/',
substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']),
0, 0-strlen($_SERVER['PHP_SELF'])));
To address files relatively use dirname(__FILE__)
:
require_once( dirname(__FILE__).'/../includes/opendb.php' );
精彩评论