Skip to content
PHP Logo

How can the dirname function be used to retrieve the directory of a file on the server in PHP?

To retrieve the directory of a file on the server in PHP, you can use the `dirname()` function. This function takes a file path as input and returns the directory component of the path. By using `dirname()` with the `__FILE__` constant, you can easily retrieve the directory of the current file on the server.

php
$file_path = __FILE__;
$directory = dirname($file_path);
echo $directory;