find the parent directory of a file in php

To find the parent directory of a file in PHP, you can use the dirname() function.

main.php
$filePath = '/path/to/my/file.txt';
$parentDir = dirname($filePath);
echo $parentDir; // Output: /path/to/my
109 chars
4 lines

In the example above, we pass the file path to the dirname() function, which returns the parent directory of the file. The resulting $parentDir variable contains the path to the parent directory.

You can also use dirname() with the __FILE__ constant to get the parent directory of the current PHP file:

main.php
$parentDir = dirname(__FILE__);
echo $parentDir;
49 chars
3 lines

This will output the path of the parent directory of the current PHP file.

gistlibby LogSnag