New to PHP so trying to work out how relative and absolute paths work with require.
My structure looks like:
/index.php
/inc/db_functions.php
/inc/config.php
/login/login.php
db_functions.php includes config.php using
require_once 'inc/config.php';
index.php includes db_functions.php using require_once 'inc/db_functions.php';
If the user is not logged in then the browser redirects to login.php.
Originally login.php was in the root folder and used the same require once as index.php. It worked perfectly.
Now it is in its own subfolder.
I changed the require_once to use
require_once('../inc/db_connect.php');
The code worked until it got to the require_once line in db_connect.php and then failed.
I changed the require once in login.php to be:
require_once(__DIR__.'/../inc/db_connect.php');
Again it failed when it got to the require_once in db_connect.php
I changed the require_once in db_connect to be an absolute path. Again it failed.
Is there any way to deal with this as it seems that the include paths for an included file are changed by the location of the calling file ( which I guess makes sense) but how to make it more robust.
db_functions.php must include config.php using
require_once 'config.php'; because they are in same folder.
In general, best would be to use full paths.
Related
I have the following dir structure:
/app
/controlers
/models
/views
init.php
config.php
/www
index.php
/www/index.php code to call the init file:
include_once '../app/init.php';
Then in /app/init.php I have:
require_once 'config.php';
require_once 'routes.php';
For some reason the config.php file is not loading. If I rename it to config2.php and change the include, it is all good. When I switch back to the original name though, it's ignored again. Is there any chance the file name is reserved somehow?
I'm using Apache coming with XAMPP.
When you want to include a file from the same directory use
require_once './config.php';
require_once './routes.php';
or
require_once __DIR__.'/config.php';
require_once __DIR__./'routes.php';
otherwise a file with same name from your include_path may get included instead...
I've been going over those two topics:
include, require and relative paths
PHP - with require_once/include/require, the path is relative to what?
and couldn't make my script to work, none of presented methods are working or maybe I'm doing something wrong.
Anyway this is where my problem occurred:
Root/ //this is root location for server
APP/ //this is root location for script
Root/APP/core/init.php //this is where I include classes and functions from
Root/APP/classes/some_class.php //this is where all classes are
Root/APP/functions/some_function.php //this is where all functions are
and so obviously I need to include init.php everywhere so I did in every file like this:
require_once 'core/init.php';
it was working until I have decided to create a location for admin files like this:
Root/APP/Admin/some_admin_file.php
and when I included init this way:
require_once '../core/init.php';
script failed to open functions, no such file in APP/Core/ folder
so I used DIR method presented in topic above and than even weirder thing happened, error:
no such file in APP/Core/classes/Admin/
What is that? :D I'm lost with this, could someone help a bit ;)
Include paths are relative to the current working directory, which can be inspected using getcwd(); this can be a source of many issues when your project becomes bigger.
To make include paths more stable, you should use the __DIR__ and __FILE__ magic constants; for instance, in your particular case:
require_once dirname(__DIR__) . '/core/init.php';
The dirname(__DIR__) expression is effectively the parent directory of the script that's currently being run.
Btw, __DIR__ could also be written as dirname(__FILE__).
Here is my directory tree:
/
index.php
include/
functions.php
head.php
connect.php
sub/
index.php
In my head.php and connect.php both have:
include_once 'include/functions.php';
My index.php in the root folder includes these two: head.php and connect.php like this:
include_once 'include/connect.php';
include_once 'include/head.php;'
However, when my index.php in sub/ includes functions.php and head.php, they would fail to also include functions.php.
Here's how I included in the sub/index.php:
include_once '../include/connect.php';
include_once '../include/head.php';
If I change in the head.php and connect.php to: include_once '../include/functions.php';
The sub/index.php would include everything normally but the index.php in the root would fail to load the functions.php.
How can I fix this?
PHP version: 5.2.*
Use the constant __DIR__ in your include statements and then move relative to that. So in sub/index.php you would do include_once __DIR__ . '../include/connect.php'
The __DIR__ is a constant that is the directory of the file that you are in.
http://php.net/manual/en/language.constants.predefined.php
If you are using php < v5.3, you can use dirname(__FILE__) to get the same thing.
The Error
Include statement error in head.php and connect.php
include_once 'include/functions.php';
The Fix
include_once 'functions.php';
OR
include_once __DIR__ . 'functions.php'; //PHP 5.3 or higher
OR
include_once dirname(__FILE__) . 'functions.php'; //PHP 5.2 or lower
The Reason
head.php and connect.php are located in the same folder as functions.php
As suggested by #Schleis, using __DIR__ (PHP 5.3+) or dirname(__FILE__); (PHP 5.2-) will allow for relative file includes.
I would suggest to set your web project root directory in every file using chdir() function, so you don't need to think about it where are you currently located and how many back-dirs ../ you need.
Use example:
chdir($_SERVER['DOCUMENT_ROOT']);
You could define constant for include path in root file and then use that constant in all other files:
define( "INCLUDE_PATH", dirname(__FILE__) . '/include/' );
// some other file
include_once INCLUDE_PATH . 'functions.php';
It is good practice to have one file like config.php in the root folder where are defined global settings like include paths etc. That way you do not have to care about relative paths anymore, and if in the future you decide to change the folder structure, instead of changing the paths in all files just change the include constant.
My directory structure looks like this:
blog -> admin -> index.php
blog.php
db.php
functions.php
I have been trying to include (require, really) blog.php in the admin/index.php, but facing lots of errors. I'm following a PHP course, and the instructor does the same thing successfully.
admin/index.php:
require "../blog.php";
which, in turn, requires two more files in its directory.
require "db.php";
require "functions.php";
If you find that relative include paths aren't working as expected, a quick fix is to prepend __DIR__ to the front of the path you're trying to include.
require __DIR__ . "/../blog.php";
It's reasonably clean, and you don't need to modify the include path or working directory.
You need to set the include_path in your php.ini.
If you want to set it at run-time, use set_include_path().
If you are including this files db.php and functions.php in index.php then you have to write this code
require "../db.php";
require "../functions.php";
OR if you are including this files in blog.php then write this code
require "db.php";
require "functions.php";
I like to start my files with chdir($_SERVER['DOCUMENT_ROOT']). This allows me to get a nice and logical base path for all my includes.
I have made a config file that includes te database information (connection, and more).
The config file is called by index.php. So I have this:
index.php->calls->config.php->calls->db.php
This works, but sometimes the config.php is not called by the index.php but by for example header.php. The problem is that the relative path in the config.php to the db.php doesn't work.
How can I solve that? Should I use absolute paths?
Thanks!
If all the files are located in the same directory, you can include it like this:
include __DIR__."/config.php";
Otherwise create a constant in your index.php page before all includes that define your application path, and use that to include your files, E.G:
in index.php:
define('APP', __DIR__);
include APP.'/config/config.php';
in config.php:
include APP.'/lib/db.php';