I'm making a website and I want to echo out some things to each page using PHP.
The simple way: include ('menu.php'); doesn't work, i get this:
Warning: include(menu.php): failed to open stream: No such file or directory in /home/folder/public_html/sitename.com/resources/page/index.php on line 82
Warning: include(menu.php): failed to open stream: No such file or directory in /home/folder/public_html/sitename.com/resources/page/index.php on line 82
Warning: include(): Failed opening 'menu.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/folder/public_html/sitename.com/resources/page/index.php on line 82
This is the method in question which works: include(dirname(__FILE__).'../../../menu.php');
This was my previous method which worked but isn't portable: chdir('/home/folder/public_html/sitename.com'); include 'menu.php';
If I'm understanding your question correctly, you're looking for a way to not have to specify the full path every time. You could use set_include_path. Something like:
set_include_path(
get_include_path() . PATH_SEPARATOR .
realpath(__DIR__ . '/../../../')
);
include('menu.php');
PHP will then search that directory whenever include is called. If you use set_include_path just once in your main or top-level script, you can then call include without the full path in every other script.
I prefer to use relative paths directly in include instead of changing the search path. I think it makes your code easier to follow.
Related
Firs of im pretty new in PHP.
I want to include a php file from root in another php file like this.
require_once "/getPublisher.php?id=' . $xy;
The file where i type this code is in '/page/page.php'.
--------------- ROOT -----------------
-page
-> page.php----------------
- trying to require_once '/getPublisher.php'
- into '/page/page.php'
-getPublisher.php-------------
I am getting these error codes back.
Warning: require_once(/users/uplade/www/getPublisher.php?id=123)
[function.require-once]: failed to open stream: No such file or
directory in /users/uplade/www/page/page.php on line 83
and
Fatal error: require_once() [function.require]: Failed opening
required '/users/uplade/www/getPublisher.php?id=123'
(include_path='.') in /users/uplade/www/page/page.php on
line 83
So how to include root files in non-root files?
EDIT:
Warning: require_once(/getPublisher.php) [function.require-once]: failed to open stream: No such file or directory in /users/uplade/www/page/page.php on line 84
WHAT IM DOING WRONG.
SOLUTION:
I removed the slash require_once 'getPublisher.php'; Thats way it worked for me!
You can't use parameters on require(). Try removing it:
require_once "/getPublisher.php";
With require_once "/getPublisher.php"; you are trying to include a file from the filesystem root, not from the document root.
If the files would be in the same directory, you would include it like this:
require_once "getPublisher.php";
But in your case, it's one level up, so just include it like this:
require_once "../getPublisher.php";
reuire and include (and their _once variants) locate a file on the filesystem based on its absolute path, falling back to a configurable set of include paths (used for placing shared libraries in standard locations).
There are two main ways to reference a file reliably when you don't know exactly where on the file system it will be (e.g. because you're going to install the software into different locations on different servers):
Relative to the "document root", that is the path configured in the web server to be the location of the URL "/". This directory is available as the superglobal variable $_SERVER['DOCUMENT_ROOT']; e.g. require $_SERVER['DOCUMENT_ROOT'] . '/getPublisher.php';
Relative to the current file, that is the file you are writing the PHP code in (regardless of how that file was accessed). This directory is available as the magic constant __DIR__; e.g. require __DIR__ . '/../getPublisher.php';
Notice in the second example I had to "climb up" from the current directory using .. which means "parent directory"; you can climb as high as you like with this: '/../../../../' etc
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 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?
What would be the reasons of a non working include_once?
Here's my folder hierarchy:
/Php/Controls/GridView/GridView.php
/Php/Controls/Form/Form.php
In "Form.php":
include_once '../GridView/GridView.php';
I'm getting this error:
Warning: include_once(../GridView/GridView.php) [function.include-once]: failed to open stream: No such file or directory in ...Form.php on line 4
Warning: include_once() [function.include]: Failed opening '../GridView/GridView.php' for inclusion (include_path='.;C:\php\pear') in ...Form.php on line 4
Please tell me if you want more information.
In Form.php use
include_once dirname(__FILE__) . '/../GridView/GridView.php';
This creates an absolute path, but relative to the file, where its called from.
It can't find the file.
You should use a full path, like /Php/Controls/GridView/GridView.php instead of a relative one.
Try this:
include_once './php/Controls/GridView/GridView.php';
Hope I'm not too late on this one but I thought this might be what you're looking for:
include_once realpath(dirname(__FILE__)."/../GridView/GridView.php");
Using realpath() allows you to include files even from outside your server document root directory.
I'm having problems assigning a relative path to require_once. I'm sure it's something simple that I'm not seeing...
folder directory structure
level 1: site
level 2: include
level 2: class
so...
site/include/global.php <-- this is where function autoload is being called from
site/class/db.php
when I attempt to use
function__autoload($className)
{
require_once '../class/'.$className.'.php';
}
i get:
Warning: require_once(../class/db.php) [function.require-once]: failed to open stream: No such file or directory
Fatal error: require_once() [function.require]: Failed opening required '../class/db.php' (include_path='.;./includes;./pear')
What am I doing wrong? It'll work fine if I plop the global.php in the class folder so I'm assuming it's my poor understanding of relative paths that is causing the problem..
Thanks
require(_once) and include(_once) work relative to the path of the first script, i.e. the script that was actually called.
If you need to require something in an included or required file, and would like to work relative to that file, use
dirname(__FILE__)."/path/to/file";
or as #Arkh points out, from PHP 5.3 upwards
__DIR__."/path/to/file";
Does this work (apache only, i believe)
require_once($_SERVER['DOCUMENT_ROOT'] . '/class/' . $classname '.php');