Problem with including a file - php

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).

Related

PHP rquire_once from root

Firs of im pretty new in PHP.
I want to include a php file from root in another php file like this.
require_once "/getPublisher.php?id=' . $xy;
The file where i type this code is in '/page/page.php'.
--------------- ROOT -----------------
-page
-> page.php----------------
- trying to require_once '/getPublisher.php'
- into '/page/page.php'
-getPublisher.php-------------
I am getting these error codes back.
Warning: require_once(/users/uplade/www/getPublisher.php?id=123)
[function.require-once]: failed to open stream: No such file or
directory in /users/uplade/www/page/page.php on line 83
and
Fatal error: require_once() [function.require]: Failed opening
required '/users/uplade/www/getPublisher.php?id=123'
(include_path='.') in /users/uplade/www/page/page.php on
line 83
So how to include root files in non-root files?
EDIT:
Warning: require_once(/getPublisher.php) [function.require-once]: failed to open stream: No such file or directory in /users/uplade/www/page/page.php on line 84
WHAT IM DOING WRONG.
SOLUTION:
I removed the slash require_once 'getPublisher.php'; Thats way it worked for me!
You can't use parameters on require(). Try removing it:
require_once "/getPublisher.php";
With require_once "/getPublisher.php"; you are trying to include a file from the filesystem root, not from the document root.
If the files would be in the same directory, you would include it like this:
require_once "getPublisher.php";
But in your case, it's one level up, so just include it like this:
require_once "../getPublisher.php";
reuire and include (and their _once variants) locate a file on the filesystem based on its absolute path, falling back to a configurable set of include paths (used for placing shared libraries in standard locations).
There are two main ways to reference a file reliably when you don't know exactly where on the file system it will be (e.g. because you're going to install the software into different locations on different servers):
Relative to the "document root", that is the path configured in the web server to be the location of the URL "/". This directory is available as the superglobal variable $_SERVER['DOCUMENT_ROOT']; e.g. require $_SERVER['DOCUMENT_ROOT'] . '/getPublisher.php';
Relative to the current file, that is the file you are writing the PHP code in (regardless of how that file was accessed). This directory is available as the magic constant __DIR__; e.g. require __DIR__ . '/../getPublisher.php';
Notice in the second example I had to "climb up" from the current directory using .. which means "parent directory"; you can climb as high as you like with this: '/../../../../' etc

Include php file

I have the following directory tree on a linux server:
/home/control-center/back-office/php/average.php
/home/control-center/front-office/db.php
db.php contains the database connection information.
When I try to execute average.php, I get the following error message:
PHP Warning: include(../../front-office/db.php): failed to open stream: No such file or directory in /home/control-center/back-office/php/average.php on line 18
PHP Warning: include(): Failed opening '../../front-office/db.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/control-center/back-office/php/average.php on line 18
Should I include something else in my script so it would work? I found a couple of similar cases on the Internet, but I haven't managed to make it work.
Try include dirname(__FILE__) . '/../../front-office/db.php';
dirname(__FILE__) returns the absolute path of the current file. This ensures that you get the correct path relative to the current file.
Your problem is often encountered when the current script was included by another script. You can check this answer which is related to what I mentioned.
How do you include these 2 pages in your avarage.php file?
if you want to include header.php in index.php page which is in "sample" folder (sample/index.php) and header.php in include/header.php, then you have to write "../" before including a directory, so that would be like:
include '../include/header.php';

unable to Include Files php using include

I have a page called index.php which includes a page new_display.php which again includes two pages i.e. say x.php and y.php
The include is working fine on localhost but not on my hosting! The error i am getting is :-
Warning: require(/interests/sketching/udis.php) [function.require]: failed to open stream: No such file or directory in /hermes/bosweb/web054/b548/ipg.pingcampuscom/mysql/interests/sketching/new_display.php on line 154
Fatal error: require() [function.require]: Failed opening required '/interests/sketching/udis.php' (include_path='.:/usr/local/lib/php-5.2.17/lib/php') in /hermes/bosweb/web054/b548/ipg.pingcampuscom/mysql/interests/sketching/new_display.php on line 154
The Directories are :
index.php -> localhost/mysql
new_display.php -> localhost/mysql/interests/sketching/new_display.php ( Included in index.php as /interests/sketching/new_display.php )
then in new_display x.php and y.php are included as
/interests/sketching/x.php
/interests/sketching/y.php
It works all good in localhost but gives error when i publish the pages on the Domain
Does anyone has any idea why isn't this working?
/ indicates an absolute path, meaning your script is looking in the root of the filesystem for a directory called interests.
Instead, try just removing that / at the start of your paths, or prepend $_SERVER['DOCUMENT_ROOT'] to them.
You have to make
require(interests/sketching/udis.php)
Without "/"
Because you have to point to the same folder ;).

Can't include my files

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);

Include one PHP file into another

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?

Categories