Can't include a file via PHP - 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"); ?>

Related

including a PHP file from localhost gives warning

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

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 is looking for a file in the wrong place

I am an amateur web developer and I am trying to get my site live for the first time. The site is working and all of the files are uploaded but the site is not loading my PHP includes.
Here is the error message:
Warning: include(includes/header.php) [function.include]: failed to open stream: No such file or directory in /home4/myUsername/public_html/index.php on line 3
How can I get PHP to look in public_html/ rather than public_html/index.php?
EDIT: I have tried editing the include path. It doesn't seem to change where php is looking for the file. Additionally my includes work properly in localhost.
I'm going to assume this is your folder structure:
public_html/index.php
public_html/includes/header.php
Generally (not always), $_SERVER['DOCUMENT_ROOT'] will now reflect the path to the base public_html directory (this I'm assuming based on the context of your message). This means you can always point to the root this way. - no matter if you have /index.php or /my/deep/file/structure.php
Try this with your include statement on index.php
<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php');
You may need to change the include path in your php.ini file or use set_include_path() to change the include path.
Here is the manual entry for the function call if you'd like to read more about it.
Have you checked already the include file?
in given. include(folder_includes/file_header.php);

php include statement unable to open stream to file in directory above current directory

I am trying to include a file in a different directory using a relative file path however I keep getting the error:
failed to open stream: No such file or directory
The path of the file calling the include statement is
wp-content/plugins/php-code-widget/execphp.php
and the file I'm trying to include is
wp-content/uploads/espresso/templates/sidebar_widgets/register_widget.php
This is my include statement:
<?php include ('../../uploads/espresso/templates/sidebar_widgets/register_widget.php ?>
Anyone see the problem?
I would avoid the relative path.
So long as WordPress hasn't changed too much since I last used it, try something like...
include ABSPATH .
'wp-content/uploads/espresso/templates/sidebar_widgets/register_widget.php';

include .php file in folder above

I am receiving this error
Warning: include(../config.php) [function.include]: failed to open stream: No such file or directory in /home/soizastu/public_html/cms/happy-api/retrieve.php on line 2
Warning: include() [function.include]: Failed opening '../config.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/soizastu/public_html/cms/happy-api/retrieve.php on line 2
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'soizastu'#'localhost' (using password: NO) in /home/soizastu/public_html/cms/happy-api/retrieve.php on line 6
Error connecting to database.
basically in the cms folder is config.php i thought include "../config.php"; would do the trick but it dosent appear so.
Try computing the absolute path and including that.
include(dirname(__FILE__)."/../config.php");
I think it's the safest bet when dealing with multiple levels of include.
Make sure that the user you're using to run your web server can read the file. For a sanity check, chmod the file to 665. If it's a Windows system, then make it so that Everyone can read the file.
This should work include('../config.php');
Just define the absolute path for the cms folder, in this case if the caller script (which you try to include the config.php) /home/soizastu/public_html/cms/happy-api/retrieve.php. Then write on top line of retrieve.php :
define('ABSPATH',dirname(dirname(__FILE__)) . '/');
and include the config.php by write :
require(ABSPATH . 'config.php');
Try this code i hope it works for me
Assume we had dbconfig.php in home directory and include that file in file that inside home like path structure.
|--admin
|--includes
|--add_dbconfig.php
|--dbconfig.php
add_dbconfig.php
enter image description hereenter image description here

Categories