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.
Related
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.
I try to include my files, but for some reason I can't...
Here is my structure:
On index.php, there is a include_once('includes/php/inc.php');. The inc.php contains these lines:
// Required classes
require_once '../classes/cl_calendar.php';
Perfect legitimate I think, but for some reason all I get is this error:
Warning: require_once(../classes/cl_calendar.php): failed to open stream: No such file or directory in /customers/xxx/xxx/httpd.www/new/extra/php/inc.php on line 14 Fatal error: require_once(): Failed opening required '../classes/cl_calendar.php' (include_path='.:/usr/share/php') in /customers/xxx/xxx/httpd.www/new/extra/php/inc.php on line 14
What makes this error to be displayed and how do I get rid of it?
The correct path is includes/classes/file.php. Why the ..? This would lead you one level over the root.
The reason for that is that even though the relative relation of the two included files may be a different one, you're including it from the index file and therefore the path has to be relative to the index file.
When you include a php file from another, it's just like taking the contents of that file and placing it in your initial (index.php) file. So, when you to require more files from the inc.php file it's being based on the path of the initial file. Try setting a base path in your index.php file and use that in your inc.php file. Try
// index.php
$base = "/var/www/htdocs/";
// or something like dirname(__FILE__) if it may change
include_once($base.'includes/php/inc.php');
// inc.php
require_once($base.'classes/cl_calendar.php');
Just Drag and drop this page cl_calendar.php to your index.php page, and and this will showing as href link then just copy full href path and paste it as it is in include.
or try this one
require_once(/../classes/cl_calendar.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');
I'm trying to include a file (/wp-load.php) at the beginning of the /html/ directory. I'm trying to include it from /wp-content/themes/pw-steel-orange/index-load.php, but I always get the error message
Warning: require_once(../wp-load.php) [function.require-once]: failed to open stream: No such file or directory in /nfs/c07/h01/mnt/102799/domains/platyworld.com/html/wp-content/themes/pw-steel-orange/index-load.php on line 1
Fatal error: require_once() [function.require]: Failed opening required '../wp-load.php' (include_path='.:/usr/local/php-5.2.6-1/share/pear') in /nfs/c07/h01/mnt/102799/domains/platyworld.com/html/wp-content/themes/pw-steel-orange/index-load.php on line 1
Am I doing something wrong? I though ../ brings the includes to the beginning directory
Sorry if this is a duplicate, I couldn't find something related to this in my searches...
You can issue the following command to see where you are pulling the file from (where you are at):
// get your current working directory
echo getcwd();
Then include the file accordingly.
// file is 3 dirs back
include '../../../wp-load.php';
If you are using a framework like CodeIgniter, for instance, there will be an index.php file at the very start of your application that will be calling all other code. So you would have to include that file according to that file.
Most frameworks use something they call a BASEPATH that is the current actual full server path to the site. This may prove very useful when migrating your site to another destination.
Here is a semi-automated way to do this:
$incPath = str_replace("/wp-content/plugins/PLUGIN_NAME","",getcwd());
ini_set('include_path', $incPath);
include('wp-load.php');
Regardless though, it's still a bad idea to include wp-load.php. ( if this link is ever deleted, See that page here )
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');