Base Directory Change Issue - php

In my app, I'm using an AJAX call to retrieve information, which requires the inclusion of a db_connect.php file. db_connect.php requires other files for it to work.
If I include db_connect.php from the AJAX file, naturally, errors are returned from includes within the db_connect.php file because db_connect.php's includes are relative to the base directory of my application.
How can I change the working directory to the base directory of the application, so the included files will function properly?
What I've tried is using the chdir function:
echo getcwd() . "<br/>";
chdir("../../");
echo getcwd();
This correctly outputs:
/my/webserver/app/lib/ajax
/my/webserver/app
However, the include errors act like I haven't just changed the directory:
Warning: include_once(functions/clean_string.func.php) [function.include-once]: failed to open stream: No such file or directory in my/webserver/app/lib/ajax/ajax.php on line 8
What am I doing wrong?

Then use absolute paths.
For example in a config file, that resides in base of the app, define a base path that is absolute.
define("BASE_PATH", dirname(__FILE__)); //This now is a ABSOLUTE path to a dir that this file is in.
Then use:
include BASE_PATH . "/dir/dir/file.php";
Then, you just have to include the config file in every script at then top relatively from where you are in the app and then just use BASE_PATH in all other includes.

getcwd() does not return your include path; instead, try something like this:
chdir("../../");
include(getcwd()."/functions/clean_string.func.php");

Related

How to get PHP to read in the contents of "textfile.txt"? [duplicate]

I have file /root/update/test.php. There's also a file, /root/connect.php; This file has a line
include "../config.php";
In /root/update/test.php. There's the code
set_include_path(".:/root");
include "connect.php";
When I run /root/update/test.php, it finds connect.php, but fails to find config.php, giving me
PHP Warning: include(../config.php): failed to open stream: No such file or directory in /root/connect.php on line 2
PHP Warning: include(): Failed opening '../config.php' for inclusion (include_path='.:/root')
This is confusing to me because the warnings make it seem like I'm doing everything correctly - the include path is /root, and it's looking for file ../config.php (/config.php), which exists. Can someone clear this up for me? Note that using absolute paths is not an option for me, due to deploying to a production server that I have no access to.
Ubuntu/Apache
You could always include it using __DIR__:
include(dirname(__DIR__).'/config.php');
__DIR__ is a 'magical constant' and returns the directory of the current file without the trailing slash. It's actually an absolute path, you just have to concatenate the file name to __DIR__. In this case, as we need to ascend a directory we use PHP's dirname which ascends the file tree, and from here we can access config.php.
You could set the root path in this method too:
define('ROOT_PATH', dirname(__DIR__) . '/');
in test.php would set your root to be at the /root/ level.
include(ROOT_PATH.'config.php');
Should then work to include the config file from where you want.
While I appreciate you believe absolute paths is not an option, it is a better option than relative paths and updating the PHP include path.
Use absolute paths with an constant you can set based on environment.
if (is_production()) {
define('ROOT_PATH', '/some/production/path');
}
else {
define('ROOT_PATH', '/root');
}
include ROOT_PATH . '/connect.php';
As commented, ROOT_PATH could also be derived from the current path, $_SERVER['DOCUMENT_ROOT'], etc.

No such file or directory (path error)

Well, the structure of site is simple:
site.com
'config' folder
config.php
cesar.php
'login' folder
index.php
index.php
Config.php:
include_once '../config/cesar.php';
At site.com/index.php:
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
At site.com/login/index.php everything is OK.
If I will remove one dot (./config/cesar.php), main index will become OK and login page will get the error.
How to make both codes work?
The .. in your path are used to go up a directory because of that the file won't exist where you are looking for it.
If you update it to be include_once 'config/cesar.php'; it should work since that will allow it to go down into the config directory rather than try to find a directory with the name of config 1 level above where index.php is located.
./ works since . is the notation for the current directory.
To answer your question, it wouldn't be possible to have the code work by using a relative path since both the files are in different locations on the server in relation to the one you want to include. If you want to have something that does then you will need to use an absolute path rather than the relative path. This would be something like /path/to/webdirectory/site.com/path/to/file/config.php (i.e. /home/charles/websites/site.com/config/config.php) in *nix and C:\path\to\webdirectory\site.com\path\to\file\config.php on windows.
In PHP you should be able to get the absolute path in a dynamic way by using the $_SERVER['DOCUMENT_ROOT'] variable. Ex: include_once $_SERVER['DOCUMENT_ROOT'] . '/config/cesar.php';
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
I ran into a similar problem once. You need to remember that (./config/xxx.php) and (config/xxx.php) mean the same thing but (../config/xxx.php) will go up a directory and find the config folder and xxx.php file in it.
You can also use $base_url as a prepend string to all your paths to make it clean. Where:
$base_url = 'http://' . $_SERVER['SERVER_NAME'];
Then replace your paths like
../config/xxx.php
with
$base_url.'/config/xxx.php'

PHP warning on include_once same file twice [duplicate]

I have file /root/update/test.php. There's also a file, /root/connect.php; This file has a line
include "../config.php";
In /root/update/test.php. There's the code
set_include_path(".:/root");
include "connect.php";
When I run /root/update/test.php, it finds connect.php, but fails to find config.php, giving me
PHP Warning: include(../config.php): failed to open stream: No such file or directory in /root/connect.php on line 2
PHP Warning: include(): Failed opening '../config.php' for inclusion (include_path='.:/root')
This is confusing to me because the warnings make it seem like I'm doing everything correctly - the include path is /root, and it's looking for file ../config.php (/config.php), which exists. Can someone clear this up for me? Note that using absolute paths is not an option for me, due to deploying to a production server that I have no access to.
Ubuntu/Apache
You could always include it using __DIR__:
include(dirname(__DIR__).'/config.php');
__DIR__ is a 'magical constant' and returns the directory of the current file without the trailing slash. It's actually an absolute path, you just have to concatenate the file name to __DIR__. In this case, as we need to ascend a directory we use PHP's dirname which ascends the file tree, and from here we can access config.php.
You could set the root path in this method too:
define('ROOT_PATH', dirname(__DIR__) . '/');
in test.php would set your root to be at the /root/ level.
include(ROOT_PATH.'config.php');
Should then work to include the config file from where you want.
While I appreciate you believe absolute paths is not an option, it is a better option than relative paths and updating the PHP include path.
Use absolute paths with an constant you can set based on environment.
if (is_production()) {
define('ROOT_PATH', '/some/production/path');
}
else {
define('ROOT_PATH', '/root');
}
include ROOT_PATH . '/connect.php';
As commented, ROOT_PATH could also be derived from the current path, $_SERVER['DOCUMENT_ROOT'], etc.

PHP require_once error with relative path

I would like to test my php application in phpunit. My problem is the require_once doesn't find the file what I would like to test. I get this error:
Warning:
require_once(C:\MyProject\phpunit-tesztek\include/../include/form.php):
failed to open stream: No such file or directory in
C:\MyProject\phpunit-tesztek\include\FormTest.php on line 4
So it search the form.php file in include/../include/form.php what is wrong.
I used this code:
require_once 'PHPUnit/Autoload.php';
require_once(__DIR__.'/../include/form.php');
the Test file is in C:\MyProject\phpunit-tesztek\include\FormTest.php
and the file what I want to test is in: C:\MyProject\include\form.php
What is the problem?
The problem is the relative path. The __DIR__ uses the directory the file is in without a trailing separator. You are then changing the path with a relative path (/../) to go up one directory.
See the manual entry -> PHP Manual for __DIR__
__DIR__ will return C:\MyProject\phpunit-tesztek\include
You are looking to use the C:\MyProject\include\ path
__DIR__ . '/../../include/Form.php';
I normally use the dirname(__FILE__) myself, as it gets the current directory of the source code file (which is absolute) allowing my relative path to move from that location.
Try removing ../
require_once(DIR.'include/form.php');
Use magic constant DIR :
require_once(__DIR__ . '/../include/form.php');

PHP include relative path

I have file /root/update/test.php. There's also a file, /root/connect.php; This file has a line
include "../config.php";
In /root/update/test.php. There's the code
set_include_path(".:/root");
include "connect.php";
When I run /root/update/test.php, it finds connect.php, but fails to find config.php, giving me
PHP Warning: include(../config.php): failed to open stream: No such file or directory in /root/connect.php on line 2
PHP Warning: include(): Failed opening '../config.php' for inclusion (include_path='.:/root')
This is confusing to me because the warnings make it seem like I'm doing everything correctly - the include path is /root, and it's looking for file ../config.php (/config.php), which exists. Can someone clear this up for me? Note that using absolute paths is not an option for me, due to deploying to a production server that I have no access to.
Ubuntu/Apache
You could always include it using __DIR__:
include(dirname(__DIR__).'/config.php');
__DIR__ is a 'magical constant' and returns the directory of the current file without the trailing slash. It's actually an absolute path, you just have to concatenate the file name to __DIR__. In this case, as we need to ascend a directory we use PHP's dirname which ascends the file tree, and from here we can access config.php.
You could set the root path in this method too:
define('ROOT_PATH', dirname(__DIR__) . '/');
in test.php would set your root to be at the /root/ level.
include(ROOT_PATH.'config.php');
Should then work to include the config file from where you want.
While I appreciate you believe absolute paths is not an option, it is a better option than relative paths and updating the PHP include path.
Use absolute paths with an constant you can set based on environment.
if (is_production()) {
define('ROOT_PATH', '/some/production/path');
}
else {
define('ROOT_PATH', '/root');
}
include ROOT_PATH . '/connect.php';
As commented, ROOT_PATH could also be derived from the current path, $_SERVER['DOCUMENT_ROOT'], etc.

Categories