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.
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.
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.phpis placed inside
http://site.com/2-demo.phpwill set the current working directory to
- Relative paths depend on the current working directory.
include "1b-demo.php"will resolve to
include "lib/SCRIPT.php"will resolve to
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 –
- If we access
http://site.com/3a-outside.php, the current working directory is set to
- But when we access
http://site.com/lib/3b-inside.php, the current working directory is set to
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
__FILE__ magic constants instead. Take note of how this works:
- Scripts are placed at
http://site.com/4a-outside.phpwill set the CWD to
- But in
__DIR__refers to where
4b-inside.phpis sitting in – Which is
__FILE__is the full absolute path of
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
- Create a
lib/config.phpto contain the database settings, secret keys, and file paths.
define("PATH_LIB", __DIR__ . DIRECTORY_SEPARATOR)will contain the absolute path of the
define("PATH_BASE", dirname(__DIR__) . DIRECTORY_SEPARATOR)will contain the absolute base path of the project (parent of
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.
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_SEPARATORif not sure, this will automatically resolve to the “correct slash”.
- Windows is not case-sensitive.
require "FOO.PHP"does not matter.
- Linux/Mac is case-sensitive.
require "FOO.PHP"refers to 2 different files.
REFERENCES & LINKS
- Official PHP manual – require
- Official PHP manual – include
- Absolute & Relative Path In PHP – Code Boxx
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!