php include from index.html - php

I'm trying to use php include to include articles on a web page... My file structure looks like this:
/Articles
/articles
App_Template.html
/tags
/new
index.html
/including
File.html
In App_Template.html I have the piece of code:
<?php include("../including/File.html"); ?>
Which works fine and displays the correct article when you access App_Template.html, However the code:
<?php include(".../including/File.html"); ?>
Within index.html in the new folder doesn't work and throws these errors:
Warning: include(.../including/File.html) [function.include]: failed to
open stream: No such file or directory in
/home/a1696768/public_html/articles/tags/new/index.html on line 79
Warning: include() [function.include]: Failed opening
'.../including/File.html' for inclusion
(include_path='.:/usr/lib/php:/usr/local/lib/php')
in /home/a1696768/public_html/articles/tags/new/index.html on line 79
I've tried searching around to find a reason for this but cannot however find any reason as to why this isn't working here but is in App_Template.html, the only thing I can possibly imagine is it's something to do with the fact the second case is an index.html?

You can't use three dots ... in paths, it's either .. for one level up or . for the current level. If you want to go 2 folders/levels up, use ../../yourfile.

Related

php Include - file in a folder

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

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

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

Categories