php include_once not working - php

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.

Related

Is this a portable way to use PHP include?

I'm making a website and I want to echo out some things to each page using PHP.
The simple way: include ('menu.php'); doesn't work, i get this:
Warning: include(menu.php): failed to open stream: No such file or directory in /home/folder/public_html/sitename.com/resources/page/index.php on line 82
Warning: include(menu.php): failed to open stream: No such file or directory in /home/folder/public_html/sitename.com/resources/page/index.php on line 82
Warning: include(): Failed opening 'menu.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/folder/public_html/sitename.com/resources/page/index.php on line 82
This is the method in question which works: include(dirname(__FILE__).'../../../menu.php');
This was my previous method which worked but isn't portable: chdir('/home/folder/public_html/sitename.com'); include 'menu.php';
If I'm understanding your question correctly, you're looking for a way to not have to specify the full path every time. You could use set_include_path. Something like:
set_include_path(
get_include_path() . PATH_SEPARATOR .
realpath(__DIR__ . '/../../../')
);
include('menu.php');
PHP will then search that directory whenever include is called. If you use set_include_path just once in your main or top-level script, you can then call include without the full path in every other script.
I prefer to use relative paths directly in include instead of changing the search path. I think it makes your code easier to follow.

Include Path issue with config and header

I have a problem with include path in PHP.
Code:
https://github.com/kukubaczek/KukuShop/blob/master/www/panel/index.php
If i open the panel/index.php page I see this error's:
Warning: require_once(/parts/header.php): failed to open stream: No
such file or directory in /PATH/KukuShop/www/panel/index.php on line 3
Fatal error: require_once(): Failed opening required
'/parts/header.php' (include_path='/..') in
/PATH/KukuShop/www/panel/index.php on line 3
How can I fix this? Here is problem with include path.
You are using absolute references when you probably want relative file locations. Try using this instead:
require_once("../parts/header.php")
What does the directory structure look like?
The set_include_path('/..'); statement doesn't seem to make sense.
Your PHP installation can't seem to find the files.

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

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

Fail to use include without getting an error:

I am trying to use this:
include_once("PoliticalForum/StoredProcedure/User/number_login_attempts.php");
Warning: include_once(../../untitled/sanitize_string.php) [function.include-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php on line 3
Warning: include_once() [function.include]: Failed opening
'../../untitled/sanitize_string.php' for inclusion
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php
on line 3
and this:
include_once("/PoliticalForum/untitled/sanitize_string.php");
Warning: include_once(/PoliticalForum/untitled/sanitize_string.php) [function.include-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php on line 3
Warning: include_once() [function.include]: Failed opening
'/PoliticalForum/untitled/sanitize_string.php' for inclusion
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php
on line 3
How can I import files safely without having those errors?
Pick one:
Provide a valid path (paths that start with / are absolute, otherwise they are relative).
Add the folder to the include_path.
Use class autoloading (if applicable).
You should make sure the files can be found on the include path, because these errors mean the file cannot be found by PHP.
More info about include can be found in the PHP manual: http://php.net/manual/en/function.include.php
I think those files you include each inlude other files (possibly with require or require_once). These files cannot be found which causes the include to fail.
Include is ignored if the file doesn't exist, but it fails if the file does exists but contains errors.
include_once("PoliticalForum/StoredProcedure/User/number_login_attempts.php");
Above statement means the directory PoliticalForum is in the same directory of the file which has this statement.
You need to look into the directory structure you are using.
Alternatively, you could use $_SERVER['DOCUMENT_ROOT'] to locate all directories starting from root directory.
ForExample:
`$_SERVER['DOCUMENT_ROOT']`/dirLevel1 // sub directory on root
Try this
include_once "PoliticalForum/StoredProcedure/User/number_login_attempts.php";
include_once"PoliticalForum/untitled/sanitize_string.php";

Categories