PHP Include File From Another Folder
Last Updated : Jan 1, 2023
IN - PHP
In this tutorial we will show you the solution of PHP include file from another folder, this is a tutorial on how to include files from other folders using PHP.
Do you need to load another file in PHP that is located in a separate folder? It's not a difficulty; the solution is straightforward.
The absolute path is the simplest approach to include a file from another folder (specify the full path to the file).
We'll go over in more depth how to include PHP files from another folder in this guide —keep reading!
Step By Step Guide On PHP Include File From Another Folder :-
Relative vs absolute file path
<?php include "1b-demo.php"; // RELATIVE PATH include "D:\\http\\1b-demo.php"; // ABSOLUTE PATH
<?php echo "It works!";
- Relative Path — A path that is based on the current working directory as a "shorthand."
- Absolute Path — The file's complete path.
- Yes, the absolute path should be self-evident. The relative path, on the other hand, is the one that most beginners get wrong.
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
- What exactly does "current working directory" mean? With getcwd(), we can quickly verify that the script is located in the correct folder. Consider the following scenario:
- 2-demo.php is located in the D:http directory.
- The current working directory is assigned to D:http when you access 2-demo.php.
- The current working directory determines relative paths.
- include "1b-demo.php" redirects to D:http1b-demo.php.
- include "lib/SCRIPT.php" redirects to D:httplibSCRIPT.php.
Current working directory
<?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
- Are you prepared for the perplexing topic of the current working directory?
- Note the locations of the scripts in –D:http3a-outside.php and D:httplib3b-inside.php.
- The current working directory is assigned to D:http when we access 3a-outside.php directly.
- However, when we directly access lib/3b-inside.php, the current working directory is set to D:httplib.
- Yes, the current working directory is set to the directory of the first script called.
- Beginners often make this mistake because they don't understand how the current working directory works.
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
- Use the __DIR__ magic constant instead if you wish to get the current script's folder.
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"; lib/5b-config.php <?php // AUTOMATIC ABSOLUTE PATH define("PATH_LIB", __DIR__ . DIRECTORY_SEPARATOR); define("PATH_BASE", dirname(__DIR__) . DIRECTORY_SEPARATOR);
- So, what is the best technique to deal with file paths? What normally do is:
- Keep the config and library files in a secured lib subdirectory and structure the project properly.
- Create a file called lib/config.php that contains the database configuration, secret keys, and file paths.
- define("PATH LIB", __DIR__) in lib/config.php will always resolve to the current lib folder.
- Then define("PATH BASE", dir( DIR__)) will resolve to the project's base folder, which is the parent of the lib folder.
- That's all there is to it; PATH LIB is required. Include PATH ROOT in "LIBRARY.PHP."
- "FILE.EXT" has now become an absolute file path that can never be incorrect.
PHP allows you to include content from another file in a running PHP script. It's essentially the same as merging two files in real-time.
This allowed you to store common functions and content in a single file and access it as needed.
I hope this tutorial on PHP include file from another folder helps you.