All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Include File From Another Folder

Last Updated : Mar 11, 2024

PHP Include File From Another Folder

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

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!";
  1. Relative Path — A path that is based on the current working directory as a "shorthand."
  2. Absolute Path — The file's complete path.
  3. 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?

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
  1. 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. 2-demo.php is located in the D:http directory.
  3. The current working directory is assigned to D:http when you access 2-demo.php.
  4. The current working directory determines relative paths.
  5. include "1b-demo.php" redirects to D:http1b-demo.php.
  6. include "lib/SCRIPT.php" redirects to D:httplibSCRIPT.php.

Current working directory

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
  1. Are you prepared for the perplexing topic of the current working directory?
  2. Note the locations of the scripts in –D:http3a-outside.php and D:httplib3b-inside.php.
  3. The current working directory is assigned to D:http when we access 3a-outside.php directly.
  4. However, when we directly access lib/3b-inside.php, the current working directory is set to D:httplib.
  5. Yes, the current working directory is set to the directory of the first script called.
  6. Beginners often make this mistake because they don't understand how the current working directory works.

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
  1. Use the __DIR__ magic constant instead if you wish to get the current script's folder.

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);
  1. So, what is the best technique to deal with file paths? What normally do is:
  2. Keep the config and library files in a secured lib subdirectory and structure the project properly.
  3. Create a file called lib/config.php that contains the database configuration, secret keys, and file paths.
  4. define("PATH LIB", __DIR__) in lib/config.php will always resolve to the current lib folder.
  5. Then define("PATH BASE", dir( DIR__)) will resolve to the project's base folder, which is the parent of the lib folder.
  6. That's all there is to it; PATH LIB is required. Include PATH ROOT in "LIBRARY.PHP."
  7. "FILE.EXT" has now become an absolute file path that can never be incorrect.

Conclusion :-

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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪