What are the potential security risks of allowing PHP to access local files on a server?

Allowing PHP to access local files on a server can pose a significant security risk as it opens the door for potential malicious attacks such as directory traversal, file inclusion vulnerabilities, and unauthorized access to sensitive files. To mitigate these risks, it is important to carefully restrict and sanitize the file paths that PHP is allowed to access.

// Example of restricting file access to a specific directory
$allowed_directory = '/path/to/allowed/directory/';
$file_path = $_GET['file'];

if (strpos(realpath($file_path), $allowed_directory) === 0) {
    // Proceed with accessing the file
    $file_contents = file_get_contents($file_path);
} else {
    // Access denied
    echo 'Access denied';
}