I have the following folder structure with files:
index.php
includes/header.php
includes/conn.php
contents/ad_list.php
contents/ad_posting.php
in my index.php I have the following include
include("includes/header.php"); its ok
but in contents/ad_list.php the above include give the following error:
Warning: include(includes/header.php) [function.include]: failed to open stream: No such file or directory in C:\XAMPP\xampp\htdocs\NAYAAD\contents\ad_list.php on line 4
I couldn't solve this problem.
Regards:
You will need to go up one level:
include("../includes/header.php");
In this case, include("../includes/header.php"); solve the problem, but, the best way is known root of your application and use it for base for includes.
You need to set include_path in your php.ini relative to your application root folder, or, try with $_SERVER['DOCUMENT_ROOT'];
Try including dirname on your inclusion.
<?php
include dirname(__FILE__)."/../contents/ad_list.php";
?>
the dirname(_FILE) can make it more portable and depending on where its running, you could just copy it up and add dots as needed.
try put a "." infrontof the address
include("./contents/ad_list.php");
Related
Well, the structure of site is simple:
site.com
'config' folder
config.php
cesar.php
'login' folder
index.php
index.php
Config.php:
include_once '../config/cesar.php';
At site.com/index.php:
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
At site.com/login/index.php everything is OK.
If I will remove one dot (./config/cesar.php), main index will become OK and login page will get the error.
How to make both codes work?
The .. in your path are used to go up a directory because of that the file won't exist where you are looking for it.
If you update it to be include_once 'config/cesar.php'; it should work since that will allow it to go down into the config directory rather than try to find a directory with the name of config 1 level above where index.php is located.
./ works since . is the notation for the current directory.
To answer your question, it wouldn't be possible to have the code work by using a relative path since both the files are in different locations on the server in relation to the one you want to include. If you want to have something that does then you will need to use an absolute path rather than the relative path. This would be something like /path/to/webdirectory/site.com/path/to/file/config.php (i.e. /home/charles/websites/site.com/config/config.php) in *nix and C:\path\to\webdirectory\site.com\path\to\file\config.php on windows.
In PHP you should be able to get the absolute path in a dynamic way by using the $_SERVER['DOCUMENT_ROOT'] variable. Ex: include_once $_SERVER['DOCUMENT_ROOT'] . '/config/cesar.php';
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
I ran into a similar problem once. You need to remember that (./config/xxx.php) and (config/xxx.php) mean the same thing but (../config/xxx.php) will go up a directory and find the config folder and xxx.php file in it.
You can also use $base_url as a prepend string to all your paths to make it clean. Where:
$base_url = 'http://' . $_SERVER['SERVER_NAME'];
Then replace your paths like
../config/xxx.php
with
$base_url.'/config/xxx.php'
I have recently made an API now for that api i have made a subdomain however it broke my php file includes
include $_SERVER['DOCUMENT_ROOT'].'/core/init.php';
now my subdomain i am using is api.website.com and i need to include something from website.com when i try to include its giving the root of the subdomain,
Summary
When i try to include init.php i get
Warning: include(/home/website/public_html/API/core/init.php): failed to open stream: No such file or directory in /home/website/public_html/api/apex/getusers.php on line 2
When i actually need
Warning: include(/home/website/public_html/core/init.php): failed to open stream: No such file or directory in /home/website/public_html/api/apex/getusers.php on line 2
I apologize for my grammar if need be.
Changing your include path to
include ("../../core/init.php");
will fix your problem. However, this is not a recommended approach.
You should think about using a dependency manager like composer. ( https://getcomposer.org/ ). Then your included files will always be under a directory like
/home/website/public_html/vendor/core
and there will be less of a chance of your application breaking if the files in /home/website/public_html/core change.
This should work.
include("../core/init.php");
Use following code
include_once $_SERVER['DOCUMENT_ROOT'] . '/../core/init.php';
visit https://developer.hyvor.com/php/including-files-in-root-with-subdomain.php to learn more.
This is my directory:
global.php
includes
class_bootstrap.php
resourcemanager
index.php
To include global.php in the file index.php, I have:
require_once('../global.php');
And in global.php, i have:
require_once(./includes/class_bootstrap.php);
When run index.php, i got this message:
Warning: require_once(./includes/class_bootstrap.php): failed to open stream: No such file or directory in C:\xampp\htdocs\yurivn\global.php on line 15
Fatal error: require_once(): Failed opening required './includes/class_bootstrap.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\yurivn\global.php on line 15
I wonder if PHP search in wrong directory for the file class_bootstrap.php, it may search for "resourcemanager/includes/class_bootstrap.php" instead of "includes/class_bootstrap.php" because if I put index.php to the same directory with global.php, it works perfectly.
Is there anyway to make index.php work in resourcemanager directory without changing anything in global.php or class_bootstrap.php? I just writing some plugin, I don't want to change anything belong to the developer.
What you really want to do (to make life easier in future) is use a definitions file
As long as this is defined before your code runs then everything will be fine - easiest way is to create a definitions.php file and then include this at the top of every page you use.
define("URL", "http://yoursite.com/"); //note the trailing / to make life easier.
Then on your includes just use
require_once(URL . 'file.php');
That way on local machine transfer to new host just change the definition or URL to
define("URL", "http://siteontheinternet.com/");
and you are good to go!
it works if you write full path like
include('D:/wamp/www/ajax/a.php');
try this
require_once('../includes/class_bootstrap.php');
Easiest way would be to set ROOT_DIR in your index or config, and then use that to resolve all other include paths.
In your index.php, try use dirname(__FILE__). So it should be:
require_once dirname(__FILE__) . 'global.php';
EDIT
Since index.php is 1 down-level from global.php, so you have to add /../, so it should be:
require_once dirname(__FILE__) . '/../global.php';
You can try below:
<?php
require_once __DIR__ . '/../global.php';
And on global.php file :
require_once __DIR__ . '/includes/class_bootstrap.php';
I got the solution! Just add this before including:
chdir('./../');
and include the global.php like they were the same directory:
require_once('./global.php');
Thank you very much for trying to help me, I really appreciate it!
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);
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?