Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/8ec582d4f7eb837f589730df1507c87b to your computer and use it in GitHub Desktop.
Save code-boxx/8ec582d4f7eb837f589730df1507c87b to your computer and use it in GitHub Desktop.
PHP Include File From Another Folder

PHP INCLUDE FILE FROM ANOTHER FOLDER

https://code-boxx.com/include-another-folder-php/

NOTES

GIST does not allow folders. Create a lib/ folder and put 3b 4b 5b inside.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?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";
<?php
echo "Hello!<br>";
<?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";
<?php
// (A) CWD IS FIXED TO THE FIRST SCRIPT!
include "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();
<?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";
<?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>";
<?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";
<?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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment