php Include - file in a folder - php

I'm using php include in the index file to include banner (graphic on my page).
The files are in the folders:
C:\Users\name\IBM\rationalsdp\workspace\RemSysTempFls\ERIC\www\apachedft\htdocs\fender400\gk\index.php
RemSysTempFls\ERIC\www\apachedft\htdocs\fender400\banner.php
I am unable to include banner.php with the following:
include ('/fender400/banner.php');
and get the errors (the page still runs):
Warning: main(/fender400/banner.php): failed to open stream: No such
file or directory in /www/apachedft/htdocs/fender400/gk/index.php on
line 16
Warning: main(): Failed opening '/fender400/banner.php' for inclusion
(include_path='.:/QOpenSys/php//lib/php') in
/www/apachedft/htdocs/fender400/gk/index.php on line 16

Are you sure that the addresses of each of those files are correct?
Try it without the initial slash and see what it tells you
include ('fender400/banner.php');
or try:
include ('../../fender400/banner.php');

Related

PHP include doesnt work on files other than index

So here is my problem:
I have an index.php in root directory in xampp's htdocs. The purpose of that is to login an user. After he loggs in, he is redirected to the game/index.php, where i have two includes:
<?php include('/game/components/language_declaration.php');
include('/game/components/currency/userdata_func.php');
?>
Here is the directory list:
index.php (which redirects to index below)
---/game/index.php (includes work)
---/game/units.php (the same includes dont work)
---/game/components/language_declaration.php
---/game/components/currency/userdata_func.php
There is a link in this index which points to units.php. And there are the same includes in units.php, except the fact that they dont work. This is the error that i get:
Warning: include(/game/components/language_declaration.php): failed to open stream: No such file or directory in E:\xampp\htdocs\game\units.php on line 7
Warning: include(): Failed opening '/game/components/language_declaration.php' for inclusion (include_path='.;\xampp\php\PEAR') in E:\xampp\htdocs\game\units.php on line 7
Warning: include(/game/components/currency/userdata_func.php): failed to open stream: No such file or directory in E:\xampp\htdocs\game\units.php on line 8
Warning: include(): Failed opening '/game/components/currency/userdata_func.php' for inclusion (include_path='.;\xampp\php\PEAR') in E:\xampp\htdocs\game\units.php on line 8
I have read about 20 posts about this problem, every single one advised to change the path, I have tried to add dots, slashes and so one, even defining the root directory. None worked. Could you help me please?
Your include statements should be like this:
---/game/index.php
<?php
include('components/language_declaration.php');
include('components/currency/userdata_func.php');
?>
---/game/units.php
<?php
include('components/language_declaration.php');
include('components/currency/userdata_func.php');
?>

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

Eclipse PHP file not able to see html file in same project folder

I'm using the Include func in a index.php file that looks for a header.html file and getting the following 2 errors:
Warning: include(includes/header.html): failed to open stream: No such
file or directory in C:\wamp\www\Projects\Chapter Three\index.php on
line 3
&
Warning: include(): Failed opening 'includes/header.html' for
inclusion (include_path='.;C:\php\pear') in
C:\wamp\www\Projects\Chapter Three\index.php on line 3
I verified both index.php & header.html are in the same dir via Explorer so what else should I check? Thanx!
You need quotes around the include URL, like include 'home.html';, If that doesnt work, try throwing a / before the url, include '/home.html';

PHP include - failed to open stream: No such file

I wrote a website in HTML but then I wanted the header and navbar portion in one file so I just had to change it once. I changed all the pages to .php and added the php includes. However now I get an error on pages that I've put in folders so I could create cruftless links.
Header file: /test/assets/header.php
Works: /test/index.php contains <?php include 'assets/header.php'; ?>
Throws Error: /test/company/index.php contains <?php include '/test/assets/header.php'; ?>
Error:
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
I am having an issue linking to the header file that is in a folder in the root folder. I believe it is a simple issue of just not knowing how to input the URL. If I attempt to include the full path to the header.php I get a URL file-access is disabled error
If you start a path with a / then it's an absolute path. Absolute mains to the actual filesystem root directory, not the virtual document root. This is why it fails.
Commonly this is what was meant:
include "$_SERVER[DOCUMENT_ROOT]/test/assets/header.php";
// Note: array key quotes only absent in double quotes context
I was also facing the same warnings:
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
I solved them with the following solution:
You have to define your path then use include function as below:
define('DOC_ROOT_PATH', $_SERVER['DOCUMENT_ROOT'].'/');
require DOC_ROOT_PATH . "../includes/pagedresults.php";
After that, you can use any directory structure.
It's not a URL; it's a filepath (although you can use full URLs when the server configuration allows it).

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