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);
Related
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.
I need to include one PHP file into another. The PHP file that needs to be included sits in a separate directory though. This is how it is set up:
folder1/global-functions.php
folder1/folder2/functions.php
I need to include the 'global-functions.php' in the 'functions.php'
I tried:
<?php include("../global-functions.php"); ?>
But this will not work. It returns this error message:
Warning: include(../global-functions.php) [function.include]: failed to open stream: No such file or directory in /home/user/public_html/wp-content/themes/folder1/folder2/custom_functions.php on line 2
Warning: include() [function.include]: Failed opening '../global-functions.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/user/public_html/wp-content/themes/folder1/folder2/custom_functions.php on line 2
Try including the file with an absolute path: something like this:
<?php include ($_SERVER['DOCUMENT_ROOT']."/folder1/global-functions.php");?>
Your original include fails because... the relative path in your include is relative to the current directory, which in your case is not "folder1/folder2/". The current directory is likely to be the page from which you are serving your content.
You need to either use an absolute path (with the help of $_SERVER['DOCUMENT_ROOT'] as in #Coomie's answer) or change your include_path to include the location of your included files (but then you must not use a relative path, but you wouldn't need to anyway).
You are including functions.php in itself. Change functions.php to global-functions.php.
And just out of curiosity, why have different files for functions? Why not make classes and objects and make your life easier?
i am getting this error
[28-Jan-2011 09:49:03] PHP Warning: require_once(../../includes/database.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/photo/application/php/classes/users.php on line 2
[28-Jan-2011 09:49:03] PHP Fatal error: require_once() [function.require]: Failed opening required '../../includes/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php') in /Applications/MAMP/htdocs/photo/application/php/classes/users.php on line 2
The file i would like to include is in
/Applications/MAMP/htdocs/photo/application/includes/database.php
I am sure the file is there, i have checked, double checked, triple checked
This is the line 2 in users.php
require_once '../../includes/database.php';
What could be the problem
Try this at the main file (e.g. index.php)
define('BASEPATH', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
and then
require_once(BASEPATH.'../../includes/database.php');
Working with full paths is always a good idea.
ps: it was an example, the path might be wrong.
The default include path is relative to the calling file (. is the path).
Example dir structure:
index.php
foo.php
bar.php
/assets/foo.php
/bar.php
if your index.php calls
include 'assets/foo.php';
and one/foo.php calls
include 'bar.php';
it will be equivalent to index.php calling
include 'assets/bar.php';
If you want your includes to all come from the same context, you'll need to use a root-relative path (#yoda has a good example of this).
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 )
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 :(';
}