PHP rquire_once from root - php

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

Related

Warning: include_once(../settings/settings.php): failed to open stream - but the path is actually right?

Warning: include_once(../settings/settings.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Project/classes/user.class.php on line 2
Warning: include_once(): Failed opening '../settings/settings.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php7.2.1/lib/php') in /Applications/MAMP/htdocs/Snapshot/classes/user.class.php on line 2
But the file location is actually right? It even gives me auto path fill suggestions (visual code plugin) to that path!
Why does this happen is this something to do with the / or the second warning line?
So I'm trying to include the settings.php file from inside the User.class.php file with the following code.
include_once("../settings/settings.php");
../ -> goes back into the root folder
settings/ -> enters settings folder
settings.php -> should locate the needed .php file
Replace
include_once("../settings/settings.php");
with
include_once(__DIR__ . "/../settings/settings.php");
__DIR__ will resolve in the absolute path of the file where this constant is used. You can navigate your folders from that.
Use an absolute path to the file. You can use $_SERVER['DOCUMENT_ROOT'] or create a const variable BASEPATH that you can easily set root directory value to.
include_once($_SERVER['DOCUMENT_ROOT']."/settings/settings.php");
OR
define('BASEPATH', "/Applications/MAMP/htdocs");
include_once(BASEPATH."/settings/settings.php");
Depending on where you call your class. Better yet is to use configuration file to fulfil the require/include path like every PHP framework does.

php require not working after load() method to refresh a div

I have inside my div a php require to
require('inc/coreturni/list.php');
that requires again a db connection
require('inc/connect.inc.php');
and they both work when I load the page.
but if I try to refresh the div, by refreshing the first include file after a click event, with jquery load()
I got the following path error:
Warning: require(inc/connect.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
why the path should be different? how could i fix it?
Your error explains it all:
Warning: require(inc/connect.inc.php): failed to open stream: No such
file or directory in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Notice how it says it can’t find inc/connect.inc.php in this directory:
C:\xampp\htdocs\taximat\inc\coreturni
It’s because your require statements are referring to relative paths. So when the file list.inc.php is loaded, the require is assuming that inc/connect.inc.php is located in C:\xampp\htdocs\taximat\inc\coreturni. Which is why it is failing.
You should set a base path in your app like so. Just a note that I am unclear on forward-slash or back-slash syntax in a case like this since I work mainly on Mac & Unix systems and the error shows windows paths, but the PHP is showing Unix paths.
$BASE_PATH='/xampp/htdocs/taximat/inc/coreturni/';
And the require all files like this:
require($BASE_PATH . 'inc/coreturni/list.php');
And like this:
require($BASE_PATH . 'inc/connect.inc.php');
That way no matter where the files are called from, the require will use an absolute path to the file. And since $BASE_PATH is a one-time variable you set you can just set it in your main config file and not worry about it. To make the code portable, just change $BASE_PATH based on system setups.
When the page is displayed for the first time, your paths are like this:
page.php
inc/
coreturni/
list.php
connect.php
In other words, all require statements will be relative as seen from page.php.
However, when you request list.php directly using AJAX, it looks more like this:
list.php
../
connect.php
Now, the require should have been for ../connect.php.
Solution
One way to solve this is by calculating the absolute path based on the file currently being processed (i.e. list.php) like this:
require dirname(__DIR__) . '/connect.php';
The dirname(__DIR__) construct will always yield the directory on your file system that's one higher up than the current script is in.

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).

require wp-load.php 3 directories back

I'm trying to include a file (/wp-load.php) at the beginning of the /html/ directory. I'm trying to include it from /wp-content/themes/pw-steel-orange/index-load.php, but I always get the error message
Warning: require_once(../wp-load.php) [function.require-once]: failed to open stream: No such file or directory in /nfs/c07/h01/mnt/102799/domains/platyworld.com/html/wp-content/themes/pw-steel-orange/index-load.php on line 1
Fatal error: require_once() [function.require]: Failed opening required '../wp-load.php' (include_path='.:/usr/local/php-5.2.6-1/share/pear') in /nfs/c07/h01/mnt/102799/domains/platyworld.com/html/wp-content/themes/pw-steel-orange/index-load.php on line 1
Am I doing something wrong? I though ../ brings the includes to the beginning directory
Sorry if this is a duplicate, I couldn't find something related to this in my searches...
You can issue the following command to see where you are pulling the file from (where you are at):
// get your current working directory
echo getcwd();
Then include the file accordingly.
// file is 3 dirs back
include '../../../wp-load.php';
If you are using a framework like CodeIgniter, for instance, there will be an index.php file at the very start of your application that will be calling all other code. So you would have to include that file according to that file.
Most frameworks use something they call a BASEPATH that is the current actual full server path to the site. This may prove very useful when migrating your site to another destination.
Here is a semi-automated way to do this:
$incPath = str_replace("/wp-content/plugins/PLUGIN_NAME","",getcwd());
ini_set('include_path', $incPath);
include('wp-load.php');
Regardless though, it's still a bad idea to include wp-load.php. ( if this link is ever deleted, See that page here )

php require_once not working the way I want it to.. relative path issue

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

Categories