wrong path using 'require' - php

Since moving my website from local to online , I have a path problem using require.
I have pages that call bootstrap.php like this:
require 'inc/bootstrap.php';
And boostrap.php looks like this:
<?php
spl_autoload_register('app_autoload');
function app_autoload($class){
require "class/$class.php";
}
This worked well in local.
Now, online, I get the following message:
Warning: require_once(/home/website/www/class/functions.php): failed to open stream: No such file or directory in /home/website/www/inc/bootstrap.php on line 6
Fatal error: require_once(): Failed opening required '/home/website/www/class/functions.php' (include_path='.:/usr/share/php:/usr/share/pear')
in /home/website/www/class/functions.php on line 6
So I thought, I should put an absolute path in boostrap.php like this:
<?php
spl_autoload_register('app_autoload');
function app_autoload($class){
$path = $_SERVER['DOCUMENT_ROOT'] . "class/$class.php";
require_once "$path";
}
But I get the exact same error.
I dont understand why it is still looking in the /home/website/www/inc/bootstrap.php and not following the absolute path which is /home/website/www/class/functions.php ?
EDIT:
After testing the different solutions using absolute pathes, I am still getting the error that "No such file or directory in /home/website/www/bootstrap.php: so it is still looking in the file instead of directory.
Could it be because I am using a double require? I first require boostrap.php from description.php (which works fine) and then I require class.php from this boostrap.php (which doesent take the absolute path but the path corresponding to the file boostrap.php ?
ANSWER:
Ok it finally works with this configuration:
First file using require:
require_once(dirname(__FILE__). '/inc/bootstrap.php');
and boostrap.php:
require_once(dirname(__FILE__). "/../class/$class.php");

As it was said in comments, it is safe to use absolute path instead of 'inc/...', cause it is relative to current class execution path, i.e.:
define ('BASE_PATH', dirname(__FILE__).'/../');
Then use BASE_PATH.{your_path} to access files.
I suggest to use require_once to avoid multiple inclusions.

Related

php require not working after load() method to refresh a div

I have inside my div a php require to
require('inc/coreturni/list.php');
that requires again a db connection
require('inc/connect.inc.php');
and they both work when I load the page.
but if I try to refresh the div, by refreshing the first include file after a click event, with jquery load()
I got the following path error:
Warning: require(inc/connect.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
why the path should be different? how could i fix it?
Your error explains it all:
Warning: require(inc/connect.inc.php): failed to open stream: No such
file or directory in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Notice how it says it can’t find inc/connect.inc.php in this directory:
C:\xampp\htdocs\taximat\inc\coreturni
It’s because your require statements are referring to relative paths. So when the file list.inc.php is loaded, the require is assuming that inc/connect.inc.php is located in C:\xampp\htdocs\taximat\inc\coreturni. Which is why it is failing.
You should set a base path in your app like so. Just a note that I am unclear on forward-slash or back-slash syntax in a case like this since I work mainly on Mac & Unix systems and the error shows windows paths, but the PHP is showing Unix paths.
$BASE_PATH='/xampp/htdocs/taximat/inc/coreturni/';
And the require all files like this:
require($BASE_PATH . 'inc/coreturni/list.php');
And like this:
require($BASE_PATH . 'inc/connect.inc.php');
That way no matter where the files are called from, the require will use an absolute path to the file. And since $BASE_PATH is a one-time variable you set you can just set it in your main config file and not worry about it. To make the code portable, just change $BASE_PATH based on system setups.
When the page is displayed for the first time, your paths are like this:
page.php
inc/
coreturni/
list.php
connect.php
In other words, all require statements will be relative as seen from page.php.
However, when you request list.php directly using AJAX, it looks more like this:
list.php
../
connect.php
Now, the require should have been for ../connect.php.
Solution
One way to solve this is by calculating the absolute path based on the file currently being processed (i.e. list.php) like this:
require dirname(__DIR__) . '/connect.php';
The dirname(__DIR__) construct will always yield the directory on your file system that's one higher up than the current script is in.

Using includes in php

I have a file that is called header (the header of my site).. I use that file in all my site. The problem is that it has this include in it:
include_once("../untitled/sanitize_string.php");
which means that a mistake may be thrown, depending on who calls the header file.
I have directories, subdirectories and subdirectories to the subdirectories.. is there a simple way to prevent this from happening.. Instead of taking the satize string include and placing it on every page and not in the header file
Warning: require_once(/untitled/sanitize_string.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7
Fatal error: require_once() [function.require]: Failed opening required '/untitled/sanitize_string.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7
You may consider setting a global include path while using include.
For php 5.3 you can do:
include_once(__DIR__ . '/../untitled/sanitize_string.php');
where __DIR__ is the directory for the current file
For older versions you can use
include_once(dirname(__FILE__) . '/../untitled/sanitize_string.php');
where __FILE__ is the path for the current file
Lets say you have the following structure:
/app/path/public/header.php
/app/path/public/user/profile.php
/app/path/untitled/sanitize_string.php
If your header.php includes santitize_script.php with a relative path like so:
include_once("../untitled/sanitize_string.php");
the php interpreter will try to include that file RELATIVELY to the current working dir so if you will do a request like http://localhost/header.php it will try to include it from /app/path/public/../untitled/sanitize_string.php and it will work.
But if you will try to do a request like http://localhost/user/profile.php and profile.php includes header.php, than header.php will try to include the file from /app/path/public/user/../untitled/sanitize_string.php and this will not work anymore. (/app/path/public/user beeing the current working dir now)
That's why is a good practice to use absolute paths when including files. When used in header.php, the __DIR__ and __FILE__ constants will always have the same values: /app/path/public and /app/path/public/header.php respectively no matter where header.php will be used thereafter
Use absolute path...
include_once('/home/you/www/include.php');
Use absolute path as yes123 said.
include_once(dirname(__FILE__)."/../untitled/sanitize_string.php");
You're going to have to use absolute paths here, as opposed to relative. I often set up some constants to represent important directories, using the old Server vars. Like so:
define('MY_DIR',$_SERVER['DOCUMENT_ROOT'].'/path/to/yer/dir');
Then, modify your include statement:
include_once(MY_DIR.'/your_file.php');

PHP is including relative to the URL path and not the file path, how do you change this?

I have a PHP file /a/b/file.php with the line
require("../connect.php");
In connect.php there is a line
require("../config.inc.php");
This all works fine on my local server. There are a bunch of files using connect.php and they all work fine too.
However on the hosting site /a/b/file.php throws an error:
Fatal error: require() [function.require]: Failed opening required
'../config.inc.php' (include_path='.:/usr/local/php5/lib/php') in
/******/connect.php on line 3
I suspect that even though connect.php is in another folder, it's looking for it relative to /a/b. Is there a php.ini setting to change this?
Why dont you use something like:
require( dirname(__FILE__).DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."connect.php");
This way you will avoid problems like when you develop an app on a Unix-Like system and deploy it on a Windows systems, or viceversa.
I don't know if there's a php.ini setting, but usually I use $_SERVER['DOCUMENT_ROOT'] for my included files.
require $_SERVER['DOCUMENT_ROOT']."/path/relative/to/document_root/file.php";
There are some cases where a relative path is better, but most of the time the files you want to include will stay in the same dir.
Usually I personally make a file called global.php (which is in the root directory of the project) where I define() a constant, include libs and so on.
<?php
// ...
define('APP_INCLUDE_DIR', dirname(__FILE__) .'/');
// ...
?>
Afterwards I include that file in all other files located in the same directory (e.g. index.php with require('global.php'). Now that everything is executed at that directory level you can use the constant APP_INCLUDE_DIR in every file which gets included.
<?php
require('global.php');
// ...
require_once(APP_INCLUDE_DIR .'a/b/c/connect.php');
?>
And in a/b/c/connect.php you could write for example
<?php
// ...
require_once(APP_INCLUDE_DIR .'a/b/config.inc.php');
?>
Check your include_path in your PHP.ini file.
http://php.net/manual/en/function.set-include-path.php
For example, on my local server (XAMPP) it looks like this: `include_path .;C:\xampp\php\PEAR
You can do a phpinfo() to find out also.

Why do I get a Warning and a Fatal error when I use ../?

When I use ../mysqlConnect.php I get the following messages.
Warning: require_once(../mysqlConnect.php) [function.require-once]:
failed to open stream: No such file or directory in /home/content/etc...
Fatal error: require_once() [function.require]: Failed opening required
'../mysqlConnect.php' (include_path='.:/usr/local/php5/lib/php') in /home/content/etc...
When I use the directory name - mydir/mysqlConnect.php - everything works fine.
require_once('../mysqlConnect.php') asks PHP to look in the directory above the one your script is currently in for mysqlConnect.php.
Since your connection file appears to be in a mydir directory, require_once('mydir/mysqlConnect.php') works because it looks in that directory, which is contained by the one it's currently in.
Visual representation (assuming script.php is your script including that file):
dir/
subdir/ # PHP looks here for ../mysqlConnect.php
script.php
mydir/ # PHP looks here for mydir/mysqlConnect.php
mysqlConnect.php
Require is relative to the invoced script, not the script you call require() in. Use something like this to have an absolute path:
require(dirname(__FILE__) . '/../mysqlConnect.php');
In PHP 5 you can also use DIR.
because it doesn't find your file then. to give a more specific answer I need to see you file-/folder-structure
That's because you are not specifying the correct include path. ../ refers to parent directory. ../../ goes two directories back, ../../../ goes three of them back. If the mysqlConnect.php file is present in the same folder as your script, you don't need to specify ../ in the include.
Make sure that you specify the correct path. You can easily check whether or not you are specifying correct path like:
if (file_exists('../mysqlConnect.php'))
{
echo 'Iam specifying the correct path !!';
}
else
{
echo 'Well, I am not :(';
}

php require_once not working the way I want it to.. relative path issue

I'm having problems assigning a relative path to require_once. I'm sure it's something simple that I'm not seeing...
folder directory structure
level 1: site
level 2: include
level 2: class
so...
site/include/global.php <-- this is where function autoload is being called from
site/class/db.php
when I attempt to use
function__autoload($className)
{
require_once '../class/'.$className.'.php';
}
i get:
Warning: require_once(../class/db.php) [function.require-once]: failed to open stream: No such file or directory
Fatal error: require_once() [function.require]: Failed opening required '../class/db.php' (include_path='.;./includes;./pear')
What am I doing wrong? It'll work fine if I plop the global.php in the class folder so I'm assuming it's my poor understanding of relative paths that is causing the problem..
Thanks
require(_once) and include(_once) work relative to the path of the first script, i.e. the script that was actually called.
If you need to require something in an included or required file, and would like to work relative to that file, use
dirname(__FILE__)."/path/to/file";
or as #Arkh points out, from PHP 5.3 upwards
__DIR__."/path/to/file";
Does this work (apache only, i believe)
require_once($_SERVER['DOCUMENT_ROOT'] . '/class/' . $classname '.php');

Categories