How can the root directory be accessed on a local Apache server in PHP?

To access the root directory on a local Apache server in PHP, you can use the $_SERVER['DOCUMENT_ROOT'] superglobal variable. This variable contains the absolute path to the root directory of the server. You can concatenate this path with the relative path of the file you want to access to get the full file path.

$root_directory = $_SERVER['DOCUMENT_ROOT'];
$file_path = $root_directory . '/path/to/file.txt';
echo $file_path;