Why include __DIR__ in the require_once? - php

For example, I always see autoloaders called like this:
require_once __DIR__ . '/../vendor/autoload.php';
What is the difference between that and the more concise
require_once '../vendor/autoload.php';
?

PHP scripts run relative to the current path (result of getcwd()), not to the path of their own file. Using __DIR__ forces the include to happen relative to their own path.
To demonstrate, create the following files (and directories):
- file1.php
- dir/
- file2.php
- file3.php
If file2.php includes file3.php like this:
include `file3.php`.
It will work fine if you call file2.php directly. However, if file1.php includes file2.php, the current directory (getcwd()), will be wrong for file2.php, so file3.php cannot be included.

The current accepted answer does not fully explain the cause of using __DIR__ and in my opinion the answer is wrong.
I am gonna explain why do we really need this.
Suppose we have a file structure like this
- index.php
- file3.php -(content: hello fake world)
- dir/
- file2.php
- file3.php - (content: hello world)
If we include file3.php from file2.php and run file2.php directly, we will see the output hello world.
Now when we include file2.php in index.php, when the code will start executing and it will see file2.php again including file3.php using include 'file3.php', at first the execution will look for file3.php in the current execution directory (which is the same directory where index.php is present)..Since file3.php is present in that directory, it will include that file3.php instead of dir/file3.php and we will see the output hello fake world instead of hello world.
If file3.php would not exist in the same directory, it would then include the correct dir/file3.php file which makes the accepted answer not valid because it states file3.php cannot be included which is not true. It is included.
However, here comes the necessity of using __DIR__. If we would use include __DIR__ . '/file3.php' in file2.php, then it would include the correct file even though another file3.php is present in the parent directory.

For include its possible to set some folders where PHP search automatically. When you include a file with a relative path you search in all of that folders. Its better to define the real path to prevent some errors in loading wrong files.
https://secure.php.net/manual/en/function.set-include-path.php
Then you can be sure that you load the correct file.

Related

Behaviour and correct use of the PHP include statement

I have the following directories and files:
└── project
├── index.php
└── lib
├── file1.php
└── file2.php
<?php
//index.php
include 'lib/file1.php';
<?php
//file1.php
include 'lib/file2.php';
<?php
//file2.php
echo 'this is from an echo statement in file2.php';
This does work when I try it in the browser as I expected. In particular I think the include statement in file1.php makes sense because it uses a path to file2.php relative to the location of index.php which includes file1.php and so is the location that file1.php's code will be executed.
However, I was surprised that if I change the include statement in file1.php to:
include 'file2.php';
it still works.
I would like to understand why both include statements work and get an idea of which of the two is the more correct.
From the documentation (emphasis mine):
If the file isn't found in the include_path, include will finally
check in the calling script's own directory and the current working
directory before failing.
file1.php's own directory is lib, and it can find file2.php there.
According to the include documentation
Files are included based on the file path given or, if none is given, the include_path specified
The PHP parser would also look at file1's directory for file2 even if it was included from index's directory.
In file1.php the correct way to include it would be
include 'file2.php';
Because it would allow you to include file1.php from anywhere, not just 'project' directory:
include 'lib/file2.php';
Would not work if you decided to create another directory and include file1.php there (../file1.php)

PHP include file that includes a file up the directory tree

I have this directory tree:
test.php
foodir
`---- foo.php
bardir
`---- bar.php
When I open test.php, I include foo.php. Then, I want foo.php to include bar.php.
test.php:
include 'foodir/foo.php';
foo.php:
include '../bardir/bar.php';
However, when I open test.php, I get:
Warning: include(../bardir/bar.php): failed to open stream: No such file or directory
I noticed that if I change my directory tree to:
test.php
foodir
`---- foo.php
---- bardir
`---- bar.php
And then change foo.php to:
include 'bardir/bar.php';
Everything works. It appears that I can include files relative to the currently included file.
However, why am I not able to travel up the directory tree of that file?
Edit:
I know that I can put include 'bardir/bar.php' in foo.php. It would search for bar.php in the location of test.php. However, that doesn't solve my problem if I include foo.php from a file in a directory other than the one where test.php is. That's because in that other directory, bardir/bar.php wouldn't exist.
When you use include 'foodir/foo.php'; that file is now basically running in the "scope" of test.php.
Therefore, the include inside that file include '../bardir/bar.php';
will go up one folder and THEN searching for bardir, which isnt there:
(it's looking for this arrangement:)
parent/test.php
parent/foodir
`---- foo.php
bardir
`---- bar.php
so, the correct include to use inside foo.php would be include 'bardir/bar.php';
in short: If you open test.php all include-paths used in any included file should be relative to the location of test.php - not to their actual location.
ps.: As mentioned in the comments: Includes are first checking the defined include-dir, only if there isn't a match, the path is considered "relative".
Simply do:
include dirname(__DIR__).'/bardir/bar.php';
__DIR__ is the absolute directory path of the file where this constant is used/called (no matter where it's included from), thus __DIR__ is equal to /full/absolute/path/foodir. and dirname(__DIR__) goes one directory up of the path so dirname(__DIR__) /full/absolute/path, and where the bar.php resides is /full/absolute/path/bardir. That's what we want, that's what we get.
This way you don't have to worry include path relativity. It will work any case, no matter where foodir/foo.php is included from
"When you use include it assume the base directory to look for
file is the directory of current file"
So when you have to include file from the another dir you have to specify the relative path from parent directory as here "Base" to include so let say for
Base -> A
->file1.php
->B
->file1.php
we will use
include "../A/file1.php";
for Adding files from A and vice-versa.
Assume ../ as the one level up of specified dir
also you can use __DIR__ like build in constants for getting the dir name of current file

PHP require, dot as prefix

What is the difference between these two in PHP?
require "./vendor/autoload.php";
vs
require "vendor/autoload.php";
For both statements the autoload.php script is found, but in certain environment the autoloader itself does not find classes. I'm not trying to solve the autoloader problem itself, but try to understand why these two make it behave differently.
The . refers to the folder that you are in, it's most a unix syntax for files them for the php. I think you should use __DIR__ to prefix the included files, so you can avoid some problems with relative paths
The . gives you the ability to set the path of the included files relatively to the path of the original file that run (the file that included them).
Lets take the following structure:
/index.php
/file2.php
/folder/
/file1.php
If index.php includes file1.php, and you want file1.php to include file2.php - you can do this using require './file2.php'; (inside file1.php, which is in the inner folder).
If you use require 'file2.php'; inside file1.php you are looking for file2.php inside the folder (which will give you an error, because the file is not there).

PHP Relative paths for require file

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

PHP: require doesn't work as it should

I have a directory root:
index.php
includes/
template.php
testfile.php
phpFiles/
processInput.php
testfile.php
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
processInput.php:
require_once("testfile.php")
require_once("../testfile.php")
This code will work when you run index.php, of course it will not work when you run template.php.
As you can see, index.php includes template.php like normal. But in template.php, you have to include like if you are in the directory that index.php is in. But then, in processInput.php, you include as if you are in the directory that processInput.php is in.
Why is this happening, and how can I fix it so that the include path is always the directory of the file that the require is done in? The second included file have the same include path as the requested file, but the next one does not.
Thanks for your help!
EDIT: The strange thing is that I've included classes in a class folder. And it included other files as it is supposed to, even though the paths are relative. WHY does this happen, and how can I fix it?
VERY IMPORTANT EDIT: I just realized that all this is because in my example, the inclusion in includes/phpFiles/processInput.php includes a file in the same directory: require_once("file in same dir.php"); This is the reason. If you are including a file with out specifying anything more than the filename, the include_path is actually the dir where the file the require is written in is in. Can anyone confirm this?
Use an absolute path.
require_once($_SERVER['DOCUMENT_ROOT']."/includes/phpFiles/processInput.php");
Use a similar form for all your required files and they will work no matter where you are.
You can do this in a few ways, amongst others:
Use set_include_path to control the directories from where to perform require() calls.
Define a common absolute base path in a constant that you define in index.php and use that in every require() statement (e.g. require(BASEPATH . '/includes/template.php')).
Use relative paths everywhere and leverage dirname(__FILE__) or __DIR__ to turn them into absolute paths. For instance: require(__DIR__ . '/phpFiles/processInput.php');
By default, the current working directory is used in the include path; you can verify this by inspecting the output of get_include_path(). However, this is not relative to where the include() is made from; it's relative to the main executing script.
You're using relative paths. You need to use absolute paths: $_SERVER['DOCUMENT_ROOT'].
When you include/require, you are basically temporarily moving all code from one file, to another.
so if file1.php (which is located in root) contains:
require("folder/file.php");
and you include file1.php in file2.php (which is in a different location (say folder directory for example):
file2.php:
require("../file1.php");
Now all of file1.php code is in file2.php. So file2.php will look like this:
require("../file1.php");
require("folder/file.php");//but because file2.php is already in the `folder` directory, this path does not exist...
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
Your directory structure is off. The file inclusion is being seen from the file you're using it from. So, "template.php" is looking for an "includes/" folder in its current folder (/includes/).
As others are saying, use absolute paths, which will make sure you're always going at it from the file system root, or use:
require_once("phpFiles/processInput.php")
In your template.php file (which is far more likely to break if you ever move things around, which is why others all recommend using absolute paths from the file system root).
BTW, if you're using "index.php" as some kind of framework system, you can consider defining a variable that stores the address of common files such as:
define('APPLICATION_PATH', realpath(dirname(__FILE__));
define('PHPFILES_PATH', APPLICAITON_PATH . '/includes/phpFiles/');

Categories