Example 1 — Get the Folder of a File
Return the parent folder, and then go up two levels.
<?php
$path = "/var/www/html/index.php";
echo dirname($path), PHP_EOL;
echo dirname($path, 2), PHP_EOL; How It Works
- We store a path with a file name at the end.
- dirname($path) removes the file name and returns the folder.
- dirname($path, 2) goes up one more level.
- Each result is printed on its own line.

