including a PHP file from localhost gives warning - php

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

Related

Can't include a file via PHP

I am uploading a navigation bar called nav.html and I would like to use PHP 7 to include this file at the top of all my pages.
This code: <?php include '/home/u783318484/public_html/nav.html';?>
returns the error message,
Warning: include(/home/u783318484/public_html/nav.html): failed to open stream: No such file or directory in /home/u783318484/public_html/apply.php on line 24
Warning: include(): Failed opening '/home/u783318484/public_html/nav.html' for inclusion (include_path='.:/opt/php-7.0/pear') in /home/u783318484/public_html/apply.php on line 24
It works fine on my PC with Apache, but when I upload it to my website (hosted by Hostinger) it does this. The nav.html is in the same folder as the other pages.
Check your path and try with this
<?php include(dirname(__FILE__).'/nav.html');?>
Use this:
<?php include ('./nav.html'); ?>
Or you could use:
<?php require ('./nav.html'); ?>
You can't include ABSOLUTE path from your root of your file system. This is for a very easy reason: when you upload your php to your server, will your server know your path? The answer is absolutelly NO.
For this reason you have to include your files as RELATIVE paths.
Take a look here: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/
Answering your question you must to this
<?php include('nav.html');?>
or if you want that this file works in every path
<?php include(dirname(__FILE__) . "/nav.html"); ?>

php include path issues in directory

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

php going up a directory -- cannot get to work

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

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

Problem with including a file

i am getting this error
[28-Jan-2011 09:49:03] PHP Warning: require_once(../../includes/database.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/photo/application/php/classes/users.php on line 2
[28-Jan-2011 09:49:03] PHP Fatal error: require_once() [function.require]: Failed opening required '../../includes/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php') in /Applications/MAMP/htdocs/photo/application/php/classes/users.php on line 2
The file i would like to include is in
/Applications/MAMP/htdocs/photo/application/includes/database.php
I am sure the file is there, i have checked, double checked, triple checked
This is the line 2 in users.php
require_once '../../includes/database.php';
What could be the problem
Try this at the main file (e.g. index.php)
define('BASEPATH', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
and then
require_once(BASEPATH.'../../includes/database.php');
Working with full paths is always a good idea.
ps: it was an example, the path might be wrong.
The default include path is relative to the calling file (. is the path).
Example dir structure:
index.php
foo.php
bar.php
/assets/foo.php
/bar.php
if your index.php calls
include 'assets/foo.php';
and one/foo.php calls
include 'bar.php';
it will be equivalent to index.php calling
include 'assets/bar.php';
If you want your includes to all come from the same context, you'll need to use a root-relative path (#yoda has a good example of this).

Categories