I have a php file outside my webroot in which I want to include a file that is inside the webroot.
folder outside webroot
- > php file in which
I put the secured data in the file named conn.txt
,
and then I used the following PHP command:
$DbInfoFile = "../conn.txt";
Try prepending a trailing slash to your full path, so it looks like
include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');
Otherwise, it will be interpreted as a relative path.
You could also try to change the dir into the webroot and see if this works - for debuggign purposes:
chdir("/home/xx/xx/domains/mydomain/webroot");
include "your_file.php";
This should work
$_SERVER['DOCUMENT_ROOT']/home/xx/xx/domains/mydomain/webroot/file-to-include.php
And make sure you have access to that level.
Full path should be:
include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');
Or you should set the path like:
include(__DIR__ . '/../webroot/file-to-include.php'); // php version >= 5.3
include(dirname(__FILE__) . '/../webroot/file-to-include.php'); // php version < 5.3
Have this in a common file, shared by all your php sources outside the webroot:
<?php
define('PATH_TO_WEBROOT', '/home/xx/xx/domains/mydomain/webroot');
And then use the following to include files.
<?php
include (PATH_TO_WEBROOT.'/file-to-include.php');
If the location of your webroot changes, you will only have to change that once in your code base.
You can configure php to automatically prepend a given file to all your scripts, by setting the auto_prepend_file
directive. That file could for instance contain the PATH_TO_WEBROOT
constant, or require_once
the file which contains it. This setting can be done on a per domain or per host basis (see the ini sections documentation).
Also, consider using the autoload feature if you are using classes extensively.