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.

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

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) ABSOLUTE VS RELATIVE FILE PATH

D:\http\1a-demo.php
<?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";
D:\http\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?

D:\http\2-demo.php
<?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 inside D:\http.
  • Accessing http://site.com/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) THE CONFUSING CURRENT WORKING DIRECTORY

D:\http\3a-outside.php
<?php
// (A) CWD IS FIXED TO THE FIRST SCRIPT!
include "lib/3b-inside.php";
D:\http\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 and D:\http\lib\3b-inside.php.
  • If we access http://site.com/3a-outside.php, the current working directory is set to D:\http.
  • But when we access http://site.com/lib/3b-inside.php, the current working directory is set to D:\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

D:\http\4a-outside.php
<?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";
D:\http\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 and D:\http\lib\4b-inside.php.
  • Accessing http://site.com/4a-outside.php will set the CWD to D:\http.
  • But in 4b-inside.php:
    • __DIR__ refers to where 4b-inside.php is sitting in – Which is D:\http\lib.
    • __FILE__ is the full absolute path of 4b-inside.php.

 

 

5) SEMI-AUTOMATIC ABSOLUTE PATH

5a-path.php
<?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";
lib/5b-config.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 the lib folder.
  • Then, define("PATH_BASE", dirname(__DIR__) . DIRECTORY_SEPARATOR) will contain the absolute base path of the project (parent of lib 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.

 

 

EXTRA BITS & LINKS

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" and require "FOO.PHP" does not matter.
  • Linux/Mac is case-sensitive. require "Foo.php" and require "FOO.PHP" refers to 2 different files.

 

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. Required fields are marked *