Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:31
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/38c3c1da9eda69ed2be3b4cd88943873 to your computer and use it in GitHub Desktop.
Save code-boxx/38c3c1da9eda69ed2be3b4cd88943873 to your computer and use it in GitHub Desktop.
PHP Absolute & Relative Path

ABSOLUTE & RELATIVE PATH IN PHP

https://code-boxx.com/php-absolute-relative-path/

NOTES

Folders are not allowed in GIST. Manually create an inside/ folder and move 3, 4, 6 to 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
echo "Hello!<br>";
<?php
// (A) ABSOLUTE PATH (FULL PATH)
require "D:\\http\\1a-dummy.php";
// (B) RELATIVE PATH (BASED ON CURRENT WORKING DIRECTORY)
// if this script is placed at "D:\http\1b-abs-rel.php"
// current working directory (cwd) will be "D:\http"
// this relative path will resolve to "D:\http\1a-dummy.php"
require "1a-dummy.php";
<?php
// (A) GET CWD
// if this script is placed at "D:\http\2-cwd.php"
// cwd will be "D:\http"
echo getcwd() . "<br>";
<?php
// (A) CWD IS BASED ON THE FIRST SCRIPT!
// if this script is placed at "D:\http\inside\3-cwd.php"
// cwd is "D:\http\inside"
require "D:\\http\\2-cwd.php";
<?php
// (A) SHOW CURRENT WORKING DIRECTORY
// if this script is placed at "D:\http\inside\4-change-cwd.php"
// cwd is "D:\http\inside"
require "D:\\http\\2-cwd.php";
// (B) CHANGE CWD TO PARENT FOLDER
// if original cwd is "D:\http\inside"
// changing the cwd to the parent will become "D:\http\"
chdir("../");
echo getcwd();
<?php
// (A) CURRENT FILE
// if this file is placed at "D:\http\5-magic.php"
// __FILE__ is "D:\http\5-magic.php"
echo __FILE__ . "<br>";
// (B) CURRENT FOLDER
// if this file is placed at "D:\http\5-magic.php"
// __DIR__ is "D:\http"
echo __DIR__ . "<br>";
<?php
// (A) CURRENT FILE
// if this file is placed at "D:\http\inside\6-magic.php"
// __FILE__ is "D:\http\inside\6-magic.php"
echo __FILE__ . "<br>";
// (B) CURRENT FOLDER
// if this file is placed at "D:\http\inside\6-magic.php"
// __DIR__ is "D:\http\inside"
echo __DIR__ . "<br>";
// (C) GET PARENT FOLDER
$parent = dirname(__DIR__) . DIRECTORY_SEPARATOR;
require $parent . "5-magic.php";
<?php
// (A) SERVER PATH VARIABLES
// if this file is placed at "D:\http\7-extra.php"
echo $_SERVER["DOCUMENT_ROOT"] . "<br>"; // D:\http
echo $_SERVER["PHP_SELF"] . "<br>"; // 7-extra.php
echo $_SERVER["SCRIPT_FILENAME"] . "<br>"; // D:/http/7-extra.php
// (B) PATH INFO
$parts = pathinfo("D:\\http\inside\\index.php");
echo $parts["dirname"] . "<br>"; // D:\http\inside
echo $parts["basename"] . "<br>"; // index.php
echo $parts["filename"] . "<br>"; // index
echo $parts["extension"] . "<br>"; // php
// (C) BASENAME
$path = "D:\\http\\inside";
echo basename($path) . "<br>"; // inside
$path = "D:\\http\\inside\\foo.php";
echo basename($path) . "<br>"; // foo.php
$path = "D:\\http\\inside\\foo.php";
echo basename($path, ".php") . "<br>"; // foo
// (D) DIRNAME
$path = "D:\\http\\inside\\";
echo dirname($path) . "<br>"; // D:\http
echo dirname($path, 2) . "<br>"; // D:\
// (E) REALPATH
echo realpath("") . "<br>"; // D:\http
echo realpath("../") . "<br>"; // D:\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment