I am trying to include a .php file, but to do so I need to go up two directories, however I cannot seem to make it work. Here is my code:
<?php include '../asset/scripts/php/portfoliohome.php'; ?>
Here is what the file structure looks like:
C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\website\pages\index.php
I am wanting to go from index.php up to website, and then I can include the .php from /asset/scripts/php/portfoliohome.php.
portfoliohome.php location:
C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\website\asset\scripts\php\portfoliohome.php
This is the error I receive:
Warning: include(.../asset/scripts/php/portfoliohome.php): failed to
open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\website\pages\index.php
on line 1
To go up 2 directories use ../ twice
<?php include '../../asset/scripts/php/portfoliohome.php'; ?>
Related
i have an index file with PHP code in localhost/KK/admin/pages
<?php
require_once 'url.php';
include 'localhost/KK/admin/pages/sidebar.php';
?>
url.php is within the same folder as index.php file. sidebar.php is in localhost/KK/admin/pages/admin/pages.
url.php has the code
<?php
$url = "localhost/KK";
?>
when i open index.php it gives me
Warning: include(localhost/KK/admin/pages/sidebar.php): failed to open
stream: No such file or directory in
C:\wamp64\www\KK\admin\pages\index.php
and
Warning: include(): Failed opening
'localhost/KK/admin/pages/sidebar.php' for inclusion
(include_path='.;C:\php\pear') in
C:\wamp64\www\KK\admin\pages\index.php
can anyone explain why i am getting these errors and why i cant call sidebar.php? also a possible solution?
Let's traverse your directory: localhost/KK/admin/pages/sidebar.php
Your script file is located here: C:\wamp64\www\KK\admin\pages\ or KK\admin\pages\
From KK\admin\pages\ you ask for a directory (thats right, a directory not your server) localhost, from localhost to KK to admin to pages.
include 'sidebar.php'; will do fine as the script is located in KK\admin\pages\ and your include as well so you can include the file relative to the location of index.php located in C:\wamp64\www\KK\admin\pages\
Instead use the relative path, ..\ to go back 1 directory or use a constant to keep track of your root.
<?php
const ROOT = 'C:\wamp64\www\';
include ROOT . 'folder\file.php';
?>
Happy new year everyone... I have been working on a project and my file structure is
index.php
functions
profile
includes
main forlder is "mysite"
I have a header and footer if I include into the index.php its fine if I include in profile/index.php I get and error
I have tried to use
include($_SERVER['DOCUMENT_ROOT'] . '/header.php');
but have had not success still errors
the error I get when trying to go to profile/index.php
Warning: include(C:/Program Files (x86)/Zend/Apache2/htdocs/header.php): failed to open stream: No such file or directory in C:\Program Files (x86)\Zend\Apache2\htdocs\mysite\profile\index.php on line 2
if you look at the top warning its trying to include from htdocs however the site main folder is mysite
Your main folder is mysite, so add mysite after $_SERVER['DOCUMENT_ROOT'] for profile/index.php the code will be like this:
include($_SERVER['DOCUMENT_ROOT'] . '/mysite/includes/header.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';
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.
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?