Welcome to a short tutorial on how to include files from other folders in PHP. Need to load another file in PHP, but it is in a different folder? No problem, it is a very simple fix.
include "C:/http/lib/script.php";
That covers the quick basics, but let us walk through more details on how “paths” work in PHP – Read on!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
PHP INCLUDE FILE PATH
All right, let us now get more into how PHP works with file paths.
1) ABSOLUTE VS RELATIVE FILE PATH
<?php
// (A) ABSOLUTE PATH (FULL PATH)
include "D:\\http\\1b-demo.php";
// (B) RELATIVE PATH (BASED ON CWD)
// if this script is placed in "D:\http\1a-demo.php"
// cwd will be "D:\http"
// this will resolve to "D:\http\1b-demo.php"
include "1b-demo.php";
<?php
echo "Hello!<br>";
First, a quick introduction to 2 terms:
- Absolute Path – The full path to the file.
- Relative Path – A “short-hand” path that is based on the current working directory.
Yep, the absolute path should be self-explanatory. But the relative path is the one that confuses most beginners.
2) WHAT IS THE CURRENT WORKING DIRECTORY?
<?php
// (A) GETCWD() TO GET THE CURRENT WORKING DIRECTORY
// if this script is placed in "D:\http\2-demo.php"
// cwd will be "D:\http"
echo getcwd() . "<br>";
// (B) ALL RELATIVE PATHS ARE BASED ON CWD
// if cwd is "D:\http"
// this will resolve to "D:\http\1b-demo.php"
include "1b-demo.php";
Just what is the “current working directory”? We can do a quick check with getcwd()
, and it is simply the folder where the script is located in. For this example:
2-demo.php
is placed insideD:\http
.- Accessing
http://site.com/2-demo.php
will set the current working directory toD:\http
. - Relative paths depend on the current working directory.
include "1b-demo.php"
will resolve toD:\http\1b-demo.php
.include "lib/SCRIPT.php"
will resolve toD:\http\lib\SCRIPT.php
.
3) THE CONFUSING CURRENT WORKING DIRECTORY
<?php
// (A) CWD IS FIXED TO THE FIRST SCRIPT!
include "lib/3b-inside.php";
<?php
// (B) THE CWD CONFUSION
// assuming files placed at - D:\http\3a-outside.php and D:\http\lib\3b-inside.php
// if accessed from "http://site.com/3a-outside.php" - cwd is "d:\http"
// if accessed from "http://site.com/lib/3b-inside.php" - cwd is "d:\http\lib"
echo getcwd();
Ready for the confusing part about the current working directory?
- Take note of where the scripts are placed –
D:\http\3a-outside.php
andD:\http\lib\3b-inside.php
. - If we access
http://site.com/3a-outside.php
, the current working directory is set toD:\http
. - But when we access
http://site.com/lib/3b-inside.php
, the current working directory is set toD:\http\lib
instead.
Yes, the current working directory is fixed to the first script. This is a common pitfall among beginners, not knowing how the current working directory works.
4) PHP MAGIC CONTANTS
<?php
// (A) RELATIVE PATH
// assuming - "D:\http\4a-outside.php"
// this will resolve to "D:\http\lib\4b-inside.php"
include "lib/4b-inside.php";
// (B) CWD VS MAGIC CONSTANTS
// assuming - "D:\http\4a-outside.php" and "D:\http\lib\4b-inside.php"
// accessing http://site.com/4a-outside.php
// cwd will be "D:\http"
// __DIR__ will be "D:\http\lib"
// __FILE__ will be "D:\http\lib\4b-inside.php"
echo getcwd() . "<br>";
echo __DIR__ . "<br>";
echo __FILE__ . "<br>";
If you want to refer to the current script itself, use the __DIR__
and __FILE__
magic constants instead. Take note of how this works:
- Scripts are placed at
D:\http\4a-outside.php
andD:\http\lib\4b-inside.php
. - Accessing
http://site.com/4a-outside.php
will set the CWD toD:\http
. - But in
4b-inside.php
:__DIR__
refers to where4b-inside.php
is sitting in – Which isD:\http\lib
.__FILE__
is the full absolute path of4b-inside.php
.
5) SEMI-AUTOMATIC ABSOLUTE PATH
<?php
// (A) LOAD CONFIG FILE
// assuming "D:\http\5a-path.php"
// this will resolve to "D:\http\lib\5b-config.php"
require __DIR__ . "/lib/5b-config.php";
echo PATH_BASE;
echo PATH_LIB;
// (B) USE PREDEFINED ABSOLUTE PATHS
include PATH_BASE . "1b-demo.php";
include PATH_LIB . "4b-inside.php";
<?php
// (C) AUTOMATIC ABSOLUTE PATH
// assuming "D:\http\lib\5b-config.php"
// PATH_LIB will be "D:\http\lib\"
// PATH_BASE will be "D:\http\"
define("PATH_LIB", __DIR__ . DIRECTORY_SEPARATOR);
define("PATH_BASE", dirname(__DIR__) . DIRECTORY_SEPARATOR);
What is the most reliable way to deal with file paths then? What I usually do:
- Structure the project properly, keep the config and library files in a protected
lib
folder. - Create a
lib/config.php
to contain the database settings, secret keys, and file paths. - In
lib/config.php
,define("PATH_LIB", __DIR__ . DIRECTORY_SEPARATOR)
will contain the absolute path of thelib
folder. - Then,
define("PATH_BASE", dirname(__DIR__) . DIRECTORY_SEPARATOR)
will contain the absolute base path of the project (parent oflib
folder).
That’s all. After loading the config file, require PATH_LIB . "LIBRARY.PHP"
and include PATH_ROOT . "FILE.EXT"
is now an absolute file path that can never go wrong.
EXTRAS
That’s all for this tutorial, and here is a small section on some extras that may be useful to you.
EXTRA) SLASHES & CASE SENSITIVE
Lastly, a small word of reminder –
- Windows uses
\
- Linux/Mac uses
/
- Does not quite matter, since modern OS will automatically parse the slash. Just use
DIRECTORY_SEPARATOR
if not sure, this will automatically resolve to the “correct slash”. - Windows is not case-sensitive.
require "Foo.php"
andrequire "FOO.PHP"
does not matter. - Linux/Mac is case-sensitive.
require "Foo.php"
andrequire "FOO.PHP"
refers to 2 different files.
REFERENCES & LINKS
- Official PHP manual – require
- Official PHP manual – include
- require_once
- include_once
- Absolute & Relative Path In PHP – Code Boxx
THE END
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
Bro your article has alot of important tips but unfortunately its completely confusing . please dont mind.
i am taking terms from your article and searching on internet to understand.
please make it more understandable.
hope you dont get angry.
Bro, your comment has a lot of important tips, but unfortunately its completely confusing. Please don’t mind.
I get confused from a confusing comment that says its confusing without mentioning what is confusing.
Please make it more understandable.
Don’t worry, I get a lot of senseless comments and find them entertaining. 😆
P.S. If everything is confusing, take it step by step. What is absolute/relative path? What is CWD? How do we change the CWD? What are magic constants? How are they different from CWD?