My project has two entry point
project (root-folder)
/config(folder)
config.php
/service(folder)
service.php
index.php
for example
File 1:/index.php ( first entry point) - here we include the config form the config folder
<?php
require_once('config/config.php');
require_once('service/service.php');
?>
File 2:service/service.php - here we include the config form the config folde
<?php
require_once('../config/config.php');
?>
if i call the File 2:service/service.php has no fatal error
but when i call the File 1:/index.php it became the fatal error as failed to require 'service/service.php' because it require again and config path is invalid
How to solve this issue.
Reason:
This issue arises because your execution starts from index.php and then you require service/service.php. Now in service.php when you do ../config/config.php, PHP tries to resolve the directory path from index.php point of view and it doesn't find any such file or directory. Hence, the error.
Solution:
Declare a constant in index.php. Check if this constant exists in service/service.php. If not, then require it, else skip it like below:
index.php:
<?php
define('INDEX_ENTRY_POINT',true);
require_once('config/config.php');
require_once('service/service.php');
?>
service.php:
<?php
if(!defined('INDEX_ENTRY_POINT')){
require_once('../config/config.php');
}
?>
Note: It is always better to use __DIR__ giving absolute paths than relative paths to avoid such issues.
You have to consider that when you call service.php from index.php, the root is that of index.php. Now there are many ways to get around this. You could decide that service.php is a main controller, just as is index.php, and thus belongs in the root folder. If for some reason you want to keep it as it is, then you have to define the root in order for it to adapt to the situation as in vivek_23 answer just above. Personally, i would keep service.php in the root folder, it is more logic.
Related
I have created a file called init.php with the following code (which is included at the top of every page)
<?php
define('APP_ROOT', 'https://example.com/myapp');
?>
I can successfully echo APP_ROOT in a traditional link, even for sub-folders, as follows
linktext
What I cannot do is use the constant with PHP Require, for neither root files or files in sub-folders, as follows
<?php require_once(APP_ROOT . '/test.php') ?>
It returns the following error message:
Fatal error: require_once(): Failed opening required 'https://example.com/myapp/test.php' (include_path='.:/usr/local/php56/pear') in...
Is it even possible to use a constant with require or include statements? If so, would appreciate any direction
If your code is running on multiple servers with different environments (locations from where your scripts run) the following idea may be useful to you:
Do not give absolute path to include files on your server.
Dynamically calculate the full path (absolute path)
Hints:
Use a combination of dirname(FILE) and subsequent calls to itself until you reach to the home of your '/index.php'. Then, attach this variable (that contains the path) to your included files.
One typical example is:
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php');
?>
https://www.php.net/manual/en/function.require-once.php
I am here because my php code keeps giving me this annoying error whenever i include something from a directory , or just including something from a file. The error is originating from includes.php. I later found that you have to add the COMPLETE path to the directory. So I did, but it just keeps giving me the same error.
My code:
include_once (__DIR__."/inc/defines.inc.php");
Note that __DIR__ gets replaced by the files own directory path. The issue here is most likely that the path from the script you include does not match the directory you want to include a file from.
A common practice is to have a file at the root of your project that defines the root path. If you include that config file (or whatever you call it), it is easier for you to include other files.
For example. imagine a project like this:
config.php
foo/bar.php
foo/baz/bat.php
Example content of config.php:
<?php
define('ROOT_PATH', __DIR__);
Now, the content of foo/baz/bat.php could be:
<?php
// Get the config file
include '../../config.php';
// Include the content of foo/bar.php
include ROOT_PATH . '/foo/bar.php';
I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.
Here's the file structure:
appCode
db.php (File I'm trying to include)
studentManagement
index.php
dep
getData.php (File I'm trying to include db.php into)
I want to include appCode/db.php in studentManagement/dep/getData.php.
getdata.php is executed with ajax from index.php
When I use:
require_once("../../appCode/db.php");
It doesn't work.
The only way it works is it I change directory first:
chdir("../");
require_once("../appCode/db.php");
Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.
Any help appreciated!!
that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)
in fact chdir has nothing to do with it, and this:
require_once("../appCode/db.php");
is the right way to call it.
always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php
If you know your entire directory all the way from root you can use:
http://php.net/manual/en/function.set-include-path.php
Now instead of using relative paths you can always include or require files starting at your include path.
You could put this in getData.php:
set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';
You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.
I know this might have been asked before but I can't figure out after reading them posts whats best for my situation. Sorry.
I'm having difficulties in declaring a file path for my include / require_once files.
My folder structure is like this.
Root folder:
index.php
Folder(core)
init.php
Folder(user)
registration.php
login.php
logout.php
....
Folder(functions)
functions.php
Folder(inc)
header.php
footer.php
So the problem I'm having is that every time I try to include or require a file lets say from Folder(core) --> file init.php in Folder(user) -> File registration.php (registration.php would be where the include command be located) it throws me an error and page doesnt load. I get an error "Warning: require_once(/core/init.php): failed to open stream: No such file or directory". Why is this happening? would be same if I try to include other files from different directories.
Please help, I know this might be a silly question but I can't figure out
If your index.php is always loaded first and loads the other files that may also load other files, then everything is relative to the root directory:
core/init.php
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!