How can PHP be used to interact with files located outside of the web root directory while maintaining security best practices?

When interacting with files located outside of the web root directory in PHP, it is important to use absolute file paths and ensure that proper permissions are set on the files and directories. One way to maintain security best practices is to store sensitive files outside of the web root directory and use PHP to access them using absolute paths.

<?php
// Define the absolute path to the file outside of the web root directory
$file_path = '/path/to/file/outside/webroot.txt';

// Check if the file exists and is readable
if (file_exists($file_path) && is_readable($file_path)) {
    // Read the contents of the file
    $file_contents = file_get_contents($file_path);
    
    // Output the file contents
    echo $file_contents;
} else {
    // Handle the case where the file is not accessible
    echo 'Unable to access file.';
}
?>