How To Include File From Another Folder In PHP

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.

The easiest way to include a file from another folder is to use the absolute path (specify the full path to the file). For example, include "C:\\http\\lib\\script.php";

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.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Include Path Useful Bits & Links
The End

 

DOWNLOAD & NOTES

First, here is the download link to the example source code as promised.

 

QUICK NOTES

If 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

D:\http\1a-demo.php
<?php
include "1b-demo.php"; // RELATIVE PATH
include "D:\\http\\1b-demo.php"; // ABSOLUTE PATH
D:\http\1b-demo.php
<?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?

D:\http\2-demo.php
<?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.php is placed inside D:\http.
  • Accessing 2-demo.php will set the current working directory to D:\http.
  • Relative paths depend on the current working directory.
    • include "1b-demo.php" will resolve to D:\http\1b-demo.php.
    • include "lib/SCRIPT.php" will resolve to D:\http\lib\SCRIPT.php.

 

 

3) CURRENT WORKING DIRECTORY – THE CONFUSION

D:\http\3a-outside.php
<?php
include "lib/3b-inside.php"; // RESOLVES TO D:\http\lib\3b-inside.php
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 –D:\http\3a-outside.php and D:\http\lib\3b-inside.php.
  • If we access 3a-outside.php directly, the current working directory is set to D:\http.
  • But when we access lib/3b-inside.php directly, the current working directory is set to D:\http\lib instead.

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

D:\http\4a-outside.php
<?php
include "lib/4b-inside.php"; // RESOLVES TO D:\http\lib\4b-inside.php
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

5a-path.php
<?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";
lib/5b-config.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 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__) will always resolve to the lib folder that it is sitting in.
  • Then, define("PATH_BASE", dir(__DIR__)) will resolve to the parent of the lib folder, the base folder of the project.

That’s all, 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

 

INFOGRAPHIC CHEAT SHEET

Include File Path in PHP (click to enlarge)

 

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!

Leave a Comment

Your email address will not be published.