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

Potential security risks of trying to access local files on a web server using PHP include exposing sensitive information, such as database credentials or configuration files, to potential attackers. This can lead to unauthorized access to the server or data leakage. To mitigate this risk, it is important to properly sanitize user input and restrict file access to only necessary directories.

// Example of how to restrict file access to a specific directory
$allowed_directory = '/var/www/html/uploads/';
$user_directory = $_GET['directory'];

if (strpos($user_directory, $allowed_directory) !== 0) {
    die('Access denied');
}

// Proceed with accessing files in the allowed directory