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 in this guide – Read on!
ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TABLE OF CONTENTS
|Download & Notes||Include Path||Useful Bits & Links|
DOWNLOAD & NOTES
First, here is the download link to the example source code as promised.
QUICK NOTESIf you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
PHP INCLUDE FILE PATH
All right, let us now get more into how PHP works with file paths.
1) RELATIVE VS ABSOLUTE FILE PATH
<?php include "1b-demo.php"; // RELATIVE PATH include "D:\\http\\1b-demo.php"; // ABSOLUTE PATH
<?php echo "It works!";
First, a quick introduction to 2 terms:
- Relative Path – A “short-hand” path that is based on the current working directory.
- Absolute Path – The full path to the file.
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 echo getcwd(); // D:\http include "1b-demo.php"; // RESOLVES TO D:\http\1b-demo.php // include "lib/SCRIPT.PHP"; RESOLVES TO D:\http\lib\SCRIPT.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
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) CURRENT WORKING DIRECTORY – THE CONFUSION
<?php include "lib/3b-inside.php"; // RESOLVES TO D:\http\lib\3b-inside.php
<?php echo getcwd(); // IF ACCESSING FROM http://site.com/3a-outside.php - D:\http // IF ACCESSING FROM http://site.com/lib/3b-inside.php - D:\http\lib
Ready for the confusing part about the current working directory?
- Take note of where the scripts are placed in –
- If we access
3a-outside.phpdirectly, the current working directory is set to
- But when we access
lib/3b-inside.phpdirectly, the current working directory is set to
Yes, the current working directory is fixed to the folder of the first script that is being called. This is a common pitfall among beginners, not knowing how the current working directory works.
4) FOLDER OF THE CURRENT SCRIPT ITSELF
<?php include "lib/4b-inside.php"; // RESOLVES TO D:\http\lib\4b-inside.php
<?php // NOT THE CURRENT WORKING DIRECTORY! // __DIR__ REFERS TO WHERE *THIS* SCRIPT IS LOCATED AT echo __DIR__; // D:/http/lib
If you want to get the folder of the current script itself, use the
__DIR__ magic constant instead.
5) SEMI-AUTOMATIC ABSOLUTE PATH
<?php // LOAD CONFIG FILE require __DIR__ . "/lib/5b-config.php"; echo PATH_BASE; // D:\http\ echo PATH_LIB; // D:\http\lib\ // USE DEFINED ABSOLUTE PATHS include PATH_BASE . "1b-demo.php"; include PATH_LIB . "4b-inside.php";
<?php // AUTOMATIC ABSOLUTE PATH 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__)will always resolve to the
libfolder that it is sitting in.
define("PATH_BASE", dir(__DIR__))will resolve to the parent of the
libfolder, the base folder of the project.
require PATH_LIB . "LIBRARY.PHP" and
include PATH_ROOT . "FILE.EXT" is now an absolute file path and can never go wrong.
EXTRA) INCLUDE & REQUIRE IS CASE SENSITIVE
<?php // LET'S SAY WE HAVE D:\http\lib\test.php // THIS WILL WORK ON WINDOWS BUT NOT ON MAC/LINUX require PATH_LIB . "TeSt.PhP";
Lastly, a small word of reminder – Windows is not case sensitive, but Mac and Linux are. Be careful with the file and folder names.
USEFUL BITS & LINKS
That’s all for this tutorial, and here is a small section on some extras that may be useful to you.
REFERENCES & LINKS
- Official PHP manual – require
- Official PHP manual – include
- Absolute & Relative Path In PHP – Code Boxx
INFOGRAPHIC CHEAT SHEET
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!