Reputation: 1568
I have a website set up in apache2 (Windows 7), say my DocumentRoot is /mysite/content
but I also have includes in a parallel directory, say /mysite/lib
so my index.php file says:
require_once("/mysite/lib/myfile.php");
but it won't find the file because it takes it from the document root. so it is looking for
/mysite/content/mysite/lib/myfile.php instead of /mysite/lib/myfile.php
and I can't make the root /mysite because the files included are referring to the root, mysite/content. any ideas how to get around this?
Upvotes: 1
Views: 461
Reputation: 6631
You can use something like this, before calling require_once("myfile.php");
:
$base = realpath(dirname(__FILE__) . '/../');
$paths = array(
$base . '/mysite/lib',
'.',
get_include_path()
);
set_include_path(implode(PATH_SEPARATOR, $paths));
Upvotes: 0
Reputation: 161
This is most likely an issue with your php.ini.
Create a file in your webroot called phpinfo.php with the content:
<?php
phpinfo();
And nothing else. Save it and then load it in your web browser, you are looking for two things
If safe_mode is on, turn it off. If open_basedir is anything other than none, set it to none. If you can't edit your php.ini for whatever reason, you may be able to set this in a .htaccess with:
php_admin_value open_basedir none
Sources:
http://www.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode-include-dir
http://www.php.net/manual/en/ini.core.php#ini.open-basedir
Upvotes: 0
Reputation: 2210
The essence of this answer is finding the current index.php position and determining the base path of your application, then by using that base path, addressing anything in your application relatively and easily.
You can get the information from php: The exemplary paths below are for an xampp server setup, you may(probably) get different results but they(I hope) will be correct.
$path = str_replace('\\','/',dirname(__FILE__));
this will give you server relative path for index.php which is for example: 'd:/xampp/htdocs/mysite/content'
then you can remove 'content' by
$path = substr($path, 0, strrpos($path,'/')+1);
now you will have something like which is your application root:
'd:/xampp/htdocs/mysite/'
now you can access lib by just saying:
require_once($path.'lib/myfile.php');
hope this is helpful.
Upvotes: 0
Reputation: 522
When including try to use a fully qualified path name, that way you will never hit those problems while requiring php files. You can use the __DIR__
constant in conjunction with realpath in order to establish kind of a root directory and then including what you need.
Let's assume the root dir is something/public and you want to include from something/libs. Assuming you are in index.php from something/public you would do the following:
$rootPath = realpath(__DIR__ . "/../");
require_once $rootPath . "/libs/somefile.php";
I think this should help your problem. On *nix systems beware of the fact that you need apache to have rights onto the libs directory in order to read from there.
Upvotes: 1