Strange issue with PHP include - php

I am using XAMPP for PHP development, new to this, was previously familiar with WampServer. I have a require_once statement like this
require_once('config.php');
I assumed it would include the file in the current directory, but it is fetching a file from PEAR directory because the path to PEAR is also set in the include_path directive in php.ini.
However if I change the include_path to just '.' which is the current directory, it seems to work fine.
This had worked fine for me before in WampServer, no clue as to what causes this (it has always looked in the current directory before fetching form include paths). Is this a problem with PHP or something to do with XAMPP? And any solutions for this?

Well, you already found the problem: the include path directive is different.
Every include is looked up relative to the include path, the first matching file is used. If you want to explicitly use a file in a specific directory, use an absolute path:
require_once __DIR__ . '/config.php';

Related

PHP relative path: can I configure it?

tl;dr: How do I make PHP interpret relative paths in include/require statement from the perspective of the current file?
This is yet another question about that old issue in PHP about relative paths. Please bear with me, as I couldn't find any solution for what I am specifically trying to do.
Consider the following directory tree and files:
[www]:
index.php
config.php
[webroot]:
home.php
index.php requires home.php, found inside webroot:
require('webroot/home.php');
home.php requires config.php, found in the parent directory:
require('../config.php');
My problem is that this won't work in my local development environment (Ubuntu 14.04 LTS / 15.10), whereas it runs flawlessly in production. Every mentioned environment is running Apache 2 and PHP 5.
Strangely, this does run locally when I run it inside my Vagrant VM (Ubuntu 12.04 LTS), accessing it from the host machine. But, right now, I cannot run a VM here.
So, why do these environments behave so differently?
This makes me believe that there must be a way to change how PHP interprets relative paths. I am currently working with a 6GB+ PHP project that is written like the example above, and I really need to avoid the amount of effort that it'll take from me to rewrite every include/require statement (using dirname(__FILE__) or so), as well as the git merge conflicts this might cause.
EDIT: I've just remembered I actually had already asked this question here: PHP: include inside included file
The path used to resolve relative URLs like this is configured by the include_path configuration option which has a dedicated function for setting it at runtime: set_include_path.
Note that the set of paths to search may include ., representing the "current working directory", which can be set with chdir and read with getcwd. You may also need to change this to make explicitly relative paths like ./foo.php and ../foo.php to work.
(I was going to recommend you used __DIR__ or $_SERVER['DOCUMENT_ROOT'] instead, but you mention that you don't want to rewrite existing code. I would still recommend to anyone else reading this to make explicit in each include where paths are relative to, to avoid odd bugs and potential security holes with the dynamic base.)
If you want to override existing functionality in place you need to either install an external library or use namespaces. Both are extra work. I'm guessing that installing an extra library probably isn't even an option.
You could try adding the paths to those folders using set_include_path.
Or you could add a global variable and several global functions like below, for all the require and include overloads, but you would still have to do a find/replace through the whole project for instances of include, require, include_once, require_once... and replace them with "include_rel"...
$include_rel_path = '.';
function include_rel($path){
global $include_rel_path;
$my_path = $include_rel_path;
//TODO maybe need to check for drive letters?
if(strpos($path, '/') === 0) { //absolutepath
$include_rel_path = preg_replace('/\/[^\/]*$/','',$path);
include($path);
} else { //relative path
$include_rel_path .= preg_replace('/\/[^\/]*$/','',$path);
include($my_path.'/'.$path);
}
$include_rel_path = $my_path;
}
You have to use auto_prepend_file. if PHP is run as an Apache module then .htaccess file to the path to your config.php file and any PHP file accessed will automatically have the contents of the config file prepended to it.
For .htaccess:
php_value auto_prepend_file /full/path/to/file/config.php
If your server is using CGI then set this directive in your php.ini or Keep in mind this ONLY will work on a server where If PHP is run as a CGI you need to add edit it in your php.ini file or put it inside a .user.ini file just without the php_value part.
auto_prepend_file /full/path/to/file/config.php
In Nginx you could add this line to server configuration inside location ~ \.php$
fastcgi_param PHP_VALUE "auto_prepend_file=/full/path/to/file/config.php";
Let me know if doesn't resolve your problem.
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/Folder/File.php";
include_once($path);
?>
That should do the trick :)
index.php:
chdir('webroot');
require_once('home.php');

php include will not work one level up

I'm trying to include a file v-functions.php that's in htdocs/sc-dev/
The file I'm working on is in htdocs/sc-dev/accounts/verify
How do I write the include for the file v-functions so I can include it in index.php which is in /verify
I tried using include ('../../sc-dev/v-functions.php'); but that wont work, although it works fine in /accounts.
I'm running php 5.3.13 on Windows 7 Home Premium using apache server
You could always use an absolute path, since you know it:
include("/htdocs/sc-dev/accounts/verify");
Alternatively, you can make use of the $_SERVER['DOCUMENT_ROOT'] superglobal, which contains the base path of the website.
In mac os, it worked with:
include('../../v-functions.php');
You can just use realpath and step by step check which location you're getting with each "../".
Anyway path to the file you want:
htdocs/sc-dev/v-functions.php
Your current path:
htdocs/sc-dev/accounts/verify/somefile.php
Leaving once ("../"):
htdocs/sc-dev/accounts/
Leaving twice ("../../"):
htdocs/sc-dev/
Picking the file ("../../v-functions.php"):
htdocs/sc-dev/v-functions.php
I hope it will help you understand.
You can use absolute path. Use $path = $_SERVER['DOCUMENT_ROOT']; to find out what your root path is then set the directory path.

PHP require_once relative to variable document root

I'm trying to include using require_once, however, I don't always know what the file structure will be relative to the DOCUMENT_ROOT...
it could be...
/config.php or /theapp/config.php or /dev/theapp/config.php or /something_else/theapp/config.php
I COULD path back from the file like require_once('../config.php') except in some cases the files may be in a symlink directory.
Basically I'm trying to find a way where NO MATTER the circumstance, any files that call the config.php file can find it.
This is what the include_path configuration setting is for. I usually set it in the Apache config for my site, or in a local .htaccess file. Use the php_value directive.
So, basically, in your Apache config file:
php_value include_path .:/var/www/where-your-site-is
Then, from your scripts, you just use:
<?php
require_once 'conf/config.php';
require_once 'views/template.php';
?>
No matter where in your sites directory structure you are.
If you have PHP 5.3+ use this:
// My page: /dir1/dir2/welcome.php
// My include: /inc/top.php
require_once(__DIR__.'/../../top.php');
From the docs: __DIR__ is the directory of the file. If used inside an include, the directory of the included file is returned.
You can use dirname(__FILE__)
It returns the complete path of your script.
example
require_once(dirname(__FILE__).'/../config/config.php');
Came up with a pretty simple solution that works when the app is running as one unit, as well as when the core system is linked in via symlink:
Instead of:
require_once('../../../config.php');
I use:
function changeDir($up_n){
$split = explode("/",$_SERVER['SCRIPT_FILENAME']);
array_pop($split); // Remove current file name
for ($i=1; $i<=$up_n; $i++){ array_pop($split); }
return implode("/",$split);
}
require_once(changeDir(3)."/config.php");
Just pop off

require once issue in php

I am using xampp to develop my php application. Few days back I installed pear ti use DB abstraction. After that, I couldn't use include files from parent directory, however I can include from sub-driectories.
Here is what I see when I check my include path
.;E:\xampp\php\PEAR
I tried changed include path using set_include_path to the location where my files are stored, then the application failed to load Pear files.
Any help appreciated.
Easiest way to prepend to the include path stack is...
set_include_path(implode(PATH_SEPARATOR, array(
'path/to/app/includes',
'path/to/any/other/includes',
get_include_path()
)));
If you really want to use set_include_path, you can do it like this:
set_include_path(get_include_path().PATH_SEPARATOR.'path_to_parent');
Use the predefined constant DIRECTORY_SEPARATOR in case your code moves to a server that uses a different directory separator.
Personally if I needed to set the path specially for a particular site, I would try to set the path in the .htaccess file in the site's web root. It provides a more obvious place to look for site-wide configurations like the include_path. Here is the line you would put in the .htaccess file:
php_value include_path ".;E:\xampp\php\PEAR;path_to_parent"
or on a Linux server:
php_value include_path ".:some_path/PEAR:path_to_parent"

How do you know the correct path to use in a PHP require_once() statement

As many do I have a config.php file in the root of a web app that I want to include in almost every other php file. So most of them have a line like:
require_once("config.php");
or sometimes
require_once("../config.php");
or even
require_once("../../config.php");
But I never get it right the first time. I can't figure out what php is going to consider to be the current working directory when reading one of these files. It is apparently not the directory where the file containing the require_once() call is made because I can have two files in the same directory that have different paths for the config.php.
How I have a situation where one path is correct for refreshing the page but an ajax can that updates part of the page requires a different path to the config.php in the require_once() statement;
What's the secret? From where is that path evaluated?
Shoot, I was afraid this wouldn't be a common problem - This is occurring under apache 2.2.8 and PHP 5.2.6 running on windows.
The current working directory for PHP is the directory in which the called script file is located. If your files looked like this:
/A
foo.php
tar.php
B/
bar.php
If you call foo.php (ex: http://example.com/foo.php), the working directory will be /A/. If you call bar.php (ex: http://example.com/B/bar.php), the working directory will be /A/B/.
There is where it gets tricky. Let us say that foo.php is such:
<?php
require_once( 'B/bar.php' );
?>
And bar.php is:
<?php
require_once( 'tar.php');
?>
If we call foo.php, then bar.php will successfully call tar.php because tar.php and foo.php are in the same directory which happens to be the working directory. If you instead call bar.php, it will fail.
Generally you will see either in all files:
require_once( realpath( dirname( __FILE__ ) ).'/../../path/to/file.php' );
or with the config file:
// config file
define( "APP_ROOT", realpath( dirname( __FILE__ ) ).'/' );
with the rest of the files using:
require_once( APP_ROOT.'../../path/to/file.php' );
I like to do this:
require_once(dirname(__FILE__)."/../_include/header.inc");
That way your paths can always be relative to the current file location.
I use the dirname(__FILE__) thing like bobwienholt most the time, but what it could pay to do is have a base entry point that loads all your other code that defines a constant refereing to the root of the project, ie
define("ROOT",dirname(__FILE__).'/' );
and then later all you need to know is where the path is relative to root, ie:
require(ROOT . "/lib/tool/error.php");
note,
you should REALLY avoid paths with "../" at the start of them, they are not relative to the file, but relative to where you ARE and this creates broken-ass code.
cd foo
php bar/baz.php
-> some error saying it cant find the file
cd bar
php baz.php
-> suddenly working.
Important
If you use "../" notation, it takes complete ignorance of the PHP Include Path, And ONLY considers where the person whom is running it is.
I include this code at the top of every page:
//get basic page variables
$self=$_SERVER['PHP_SELF'];
$thispath=dirname($_SERVER['PHP_SELF']);
$sitebasepath=$_SERVER['DOCUMENT_ROOT'];
//include the global settings, variables and includes
include_once("$sitebasepath/globals/global.include.php");
Include and require both take either a relative path or the full rooted path. I prefer working with the full path and make all my references like the inlcude statement above. This allows me to enter a general variable $sitebasepath that handles account specific information that may change from machine to machine and then simply type the path from the webroot, ie. /globals/whatever_file.php
I also use the $self variable in forms that may call themselves to handle data input.
Hope that helps.
If you have sufficient access rights, try to modify PHP's include_path setting for the whole site. If you cannot do that, you'll either have to route every request through the same PHP script (eg. using Apache mod_rewrite) or you'll have to use an "initialization" script that sets up the include_path:
$includeDir = realpath(dirname(__FILE__) . '/include');
ini_set('include_path', $includeDir . PATH_SEPARATOR . ini_get('include_path'));
After that file is included, use paths relative to the include directory:
require_once '../init.php'; // The init-script
require_once 'MyFile.php'; // Includes /include/MyFile.php
Since require and require_once are very similar to include and include_once, all the documentation is posted under the "include" functions doc area on php.net From that page
Files for including are first looked
for in each include_path entry
relative to the current working
directory, and then in the directory
of current script. E.g. if your
include_path is libraries, current
working directory is /www/, you
included include/a.php and there is
include "b.php" in that file, b.php
is first looked in /www/libraries/
and then in /www/include/. If filename
begins with ./ or ../, it is looked
only in the current working directory.
Further, you can find all the current include paths by doing a "php -i" from the command line. You can edit the include path in your php.ini file, and also via ini_set(). You can also run the php_info() function in your page to get a printout of your env vars if the CLI is inconvenient.
The only place I've seen the path evaluated from is the file that you are currently editing. I've never had any problems with it, but if you are, you might want to provide more information (PHP version, OS, etc).
The path of the PHP file requested in the original GET or POST is essentially the 'working directory' of that script. Any "included" or "required" scripts will inherit that as their working directory as well.
I will either use absolute paths in require statements or modify PHP's include_path to include any path in my app I may want to use to save me the extra typing. You'll find that in php.ini.
include_path = ".:/list/of/paths/:/another/path/:/and/another/one"
I don't know if it'll help you out in this particular instance but the magical constants like FILE and DIR can come in handy if you ever need to know the path a particular file is running in.
http://us2.php.net/manual/en/language.constants.predefined.php
Take a look at the function getcwd. http://us2.php.net/getcwd

Categories