Include php file - php

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

Related

File is not including and require

I include a file in my php document but on local side and on server side it is not including i use require but it is also not working and an error show on the screen.. I checked the path 100 times it is right. The error is this:
Warning: require(../library/config.php) [function.require]: failed to open stream: No such file or directory in /home/logics/public_html/clients/creativecellutions/farnelo/files/header.php on line 3
Warning: require(../library/config.php) [function.require]: failed to open stream: No such file or directory in /home/logics/public_html/clients/creativecellutions/farnelo/files/header.php on line 3
Fatal error: require() [function.require]: Failed opening required '../library/config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/logics/public_html/clients/creativecellutions/farnelo/files/header.php on line 3
Please help me to resolve this.
This is my code:
<?php
require ("../library/config.php");
require ("../library/functions.php");
?>
require(../library/config.php)
Is pointing to the wrong location in your directory. Get the location of config.php relative to the position of the file this script is calling. That should resolve the error.
your code will try to include a file located at
/home/logics/public_html/clients/creativecellutions/farnelo/library/config.php
are you sure that is the currect location of the file?
if it is then the only ussue possible is that you do not have permission to read the file with php.
I guess you are not directly running clients/creativecellutions/farnelo/files/header.php and including this in some other file index.php or something else and that's where the problem may be.
Try using relative path require_once( dirname(__FILE__) . '/../library/config.php' );
Try using getcwd() function.
That is
include(getcwd()."path to config.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?

php include_once not working

What would be the reasons of a non working include_once?
Here's my folder hierarchy:
/Php/Controls/GridView/GridView.php
/Php/Controls/Form/Form.php
In "Form.php":
include_once '../GridView/GridView.php';
I'm getting this error:
Warning: include_once(../GridView/GridView.php) [function.include-once]: failed to open stream: No such file or directory in ...Form.php on line 4
Warning: include_once() [function.include]: Failed opening '../GridView/GridView.php' for inclusion (include_path='.;C:\php\pear') in ...Form.php on line 4
Please tell me if you want more information.
In Form.php use
include_once dirname(__FILE__) . '/../GridView/GridView.php';
This creates an absolute path, but relative to the file, where its called from.
It can't find the file.
You should use a full path, like /Php/Controls/GridView/GridView.php instead of a relative one.
Try this:
include_once './php/Controls/GridView/GridView.php';
Hope I'm not too late on this one but I thought this might be what you're looking for:
include_once realpath(dirname(__FILE__)."/../GridView/GridView.php");
Using realpath() allows you to include files even from outside your server document root directory.

Problem with including a file

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

include .php file in folder above

I am receiving this error
Warning: include(../config.php) [function.include]: failed to open stream: No such file or directory in /home/soizastu/public_html/cms/happy-api/retrieve.php on line 2
Warning: include() [function.include]: Failed opening '../config.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/soizastu/public_html/cms/happy-api/retrieve.php on line 2
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'soizastu'#'localhost' (using password: NO) in /home/soizastu/public_html/cms/happy-api/retrieve.php on line 6
Error connecting to database.
basically in the cms folder is config.php i thought include "../config.php"; would do the trick but it dosent appear so.
Try computing the absolute path and including that.
include(dirname(__FILE__)."/../config.php");
I think it's the safest bet when dealing with multiple levels of include.
Make sure that the user you're using to run your web server can read the file. For a sanity check, chmod the file to 665. If it's a Windows system, then make it so that Everyone can read the file.
This should work include('../config.php');
Just define the absolute path for the cms folder, in this case if the caller script (which you try to include the config.php) /home/soizastu/public_html/cms/happy-api/retrieve.php. Then write on top line of retrieve.php :
define('ABSPATH',dirname(dirname(__FILE__)) . '/');
and include the config.php by write :
require(ABSPATH . 'config.php');
Try this code i hope it works for me
Assume we had dbconfig.php in home directory and include that file in file that inside home like path structure.
|--admin
|--includes
|--add_dbconfig.php
|--dbconfig.php
add_dbconfig.php
enter image description hereenter image description here

Categories