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!
Related
I am trying to use absolute paths to get a definitive reference to a config file.
For this, I am currently doing this:
require_once '../config.php'; //Works fine
But when I try this:
require_once '/config.php'; //Throws error, see below
The error I get is:
require_once(): Failed opening required '/config.php' (include_path='.;C:\php\pear')
Is this a thing in PHP 5.6.0 or am I missing something?
Breaking down ../config.php:
../ goes one directory above the working directory
config.php finds the file config.php in that directory
Breaking down /config.php:
/ goes to the server root
config.php finds the file config.php in that directory
If the first one works but not the second, it means that the file config.php is not in the root folder, but is in the parent directory of the working file.
If you explicitly set the includes path in your script to the actual location it should be quite simple to include the files you need such as:
set_include_path( $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR );
require_once( 'config.php' );
I have a question about require_once in php.
First this my folder structure:
monsite.dev
classes
Database.inc
Users.inc
UsersProfile.inc
core
Config.inc
web
Index.php
tester.php
Database.inc to connect to my database
web/Index.php for home page
tester.php just for testing thing
I use require_once to link file together, which works almost very good except for the tester.php, which keeps telling me "failed to open the stream".
I tried 2 different way in tester.php:
1 - require_once '../classes/database.inc'; (failed to open Database.inc)
2 - require_once 'Users/Nicks/Sites/monsite/classes/database.inc';(failed to open Config.inc)
But when I use it from index.php (require_once ../classes/database.inc) it works perfectly.
Why is this not working from tester.php while it works from index.php?
Many thanks for any help.
Nic
require_once 'classes/Database.inc';
You can use this because file is on same directory.
That is because that is the incorrect path. In tester.php it should be:
require_once __DIR__ . "/classes/Database.inc";
Using ../ goes back up a level. Therefore in your directory structure, you're saying that the classes/ directory is alongside the monsite.dev directory. Not inside it.
When used in index.php, which is in a subdirectory, you have to go back up a level - which is why it works from there.
tester.php & classes directory is on same folder so no need of ../. Try with -
require_once 'classes/Database.inc';
I have this condition :
a file : /public_html/folderX/test.php has a line : require_once '../functions/sendemail.php'
on the other hand, /public_html/functions/sendemail.php has a line : require_once '../config.php'
config.php loads perfectly in this situation.
the problem occurs when I try to add that functions/sendemail.php on file(s) which not in the folderX, for example :
when I tried to add require_once 'functions/sendemail.php' on public_html/test.php I got this error message :
Warning: require_once(../config-min.php) [function.require-once]: failed to open stream: No such file or directory in public_html/test.php
how to make require_once '../config.php' inside functions/sendemail.php works 'independently' so wherever it's included on any files this 'require_once' problem won't occur anymore.
I tried to change into 'include_once' but still doesn't work.
thanks!
try something like
require_once( dirname(__FILE__).'/../config.php')
Try using __DIR__ to attain the current path of the script.
require_once(__DIR__.'../config.php');
__DIR__ only works on php 5.3
__DIR__
The directory of the file. If used inside an include, the directory of
the included file is returned. This is equivalent to dirname(__FILE__).
This directory name does not have a trailing slash unless it is the root directory.
(Added in PHP 5.3.0.)
I believe the relative path names are biting you here. Relative paths are (to my knowledge) based on the directory of the currently active script. PHP doesn't chdir into a folder when including or requiring files. The best recommendation (in my limited experience) for this kind of thing is to use absolute paths where possible. So something like:
require_once('../config.php');
would become:
require_once('/home/myuser/config.php'); // Or wherever the file really is
The dirname function can help in this situation.
You must understand that PHP changes directory to that of the outermost script. When you use relative paths (e.g. those that begin with ./, ../, or those that do not begin with /), PHP will use the current directory to resolve the relative paths. This causes problem when you copy-paste the include lines in your code. Consider this directory structure:
/index.php
/admin/index.php
/lib/include.php
Assume the two index files contain these lines:
include_once("lib/include.php");
The above line will work when /index.php is called but not when /admin/index.php is called.
The solution is to not copy-paste code, use correct relative file paths in your include calls:
/index.php -> include_once("lib/include.php");
/admin/index.php -> include_once("../lib/include.php");
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");
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');