include .php file in folder above - php

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

Related

Warning: include_once(../settings/settings.php): failed to open stream - but the path is actually right?

Warning: include_once(../settings/settings.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Project/classes/user.class.php on line 2
Warning: include_once(): Failed opening '../settings/settings.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php7.2.1/lib/php') in /Applications/MAMP/htdocs/Snapshot/classes/user.class.php on line 2
But the file location is actually right? It even gives me auto path fill suggestions (visual code plugin) to that path!
Why does this happen is this something to do with the / or the second warning line?
So I'm trying to include the settings.php file from inside the User.class.php file with the following code.
include_once("../settings/settings.php");
../ -> goes back into the root folder
settings/ -> enters settings folder
settings.php -> should locate the needed .php file
Replace
include_once("../settings/settings.php");
with
include_once(__DIR__ . "/../settings/settings.php");
__DIR__ will resolve in the absolute path of the file where this constant is used. You can navigate your folders from that.
Use an absolute path to the file. You can use $_SERVER['DOCUMENT_ROOT'] or create a const variable BASEPATH that you can easily set root directory value to.
include_once($_SERVER['DOCUMENT_ROOT']."/settings/settings.php");
OR
define('BASEPATH', "/Applications/MAMP/htdocs");
include_once(BASEPATH."/settings/settings.php");
Depending on where you call your class. Better yet is to use configuration file to fulfil the require/include path like every PHP framework does.

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

php configuration error

My php files were working fine but today when i check them i get following errors.
Warning: require_once(Connections/deal.php) [function.require-once]: failed to open stream: No such file or directory in C:\complete project\ordering system\orderingcart.php on line 1
Fatal error: require_once() [function.require]: Failed opening required 'Connections/orders.php' (include_path='.;C:\php\pear') in C:\complete project\orderingcart.php on line 1
I don't know how to resolve these errors, where do I start?
It means, the path you gave in require_once() is invalid. You have two options,
1.Checkout whether the path Connections/deal.php is correct or not. Do you have a folder Connections in the folder where this running file is placed? Is there a deal.php inside that folder Connections?
I think your running file path is C:\complete project\ordering system\orderingcart.php. It does not have such a folder and file. So place your Connections folder in C:\complete project\ordering system\ and checkout.
2.Else, You can run the page without fatal error if you change the line to include_once(Connections/deal.php). Functions assosciated with deal.php will be inactivated.
This sounds like the file has either moved OR the file permission have been altered as to prevent the server account (usually www-data) from having access to it.
You might also have better luck by trying to access the file by suppling an absolute path, rather than a relative path, e.g:
<?php
$lines = file("C:\\Documents and Settings\\myfile.txt");
foreach($lines as $line){
echo($line);
}
?>
This file path issue give full file location like
if it is windows
require_once('C:/complete project/Connections/deal.php');
For reference
http://php.net/manual/en/function.realpath.php

PHP Include not working into my coding

I am running a website locally.
I have one main folder, which contains all main coding for normal users, it is a file named main.php.
Inside this main folder I have another folder for the admin, which contains, for example, the file named announcement2.php. However, when I try to include it in the front page (index.php), an error appears, it is like the server cannot read the file inside the admin folder.
The error is:
Warning: include(../admin/announcement2.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\pods\main.php on line 85
Warning: include() [function.include]: Failed opening '../admin/announcement2.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\pods\main.php on line 85
And here is the code to include the file in the admin folder from main.php
<td><?php include '../admin/announcement2.php'; ?></td>
This probably just means either (a) permissions are incorrect (web server can't read the file), or more likely, (b) the path is incorrect. If you echo $_SERVER['PHP_SELF']; you'll see the path of the script, which would shed light on what path to actually use. But it's usually faster to try a few variations ...
include '../admin/announcement2.php';
include 'admin/announcement2.php';
include '../../admin/announcement2.php';
... until something works.

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.

Categories