What are the potential pitfalls of trying to access the filesystem using PHP scripts?

One potential pitfall of trying to access the filesystem using PHP scripts is the risk of exposing sensitive information or compromising security if proper precautions are not taken. To mitigate this risk, it is important to sanitize user input, validate file paths, and restrict access to only necessary directories.

// Example of sanitizing user input before accessing the filesystem
$filename = $_GET['filename'];
$allowed_extensions = ['jpg', 'png', 'gif'];

// Check if the file extension is allowed
if(in_array(pathinfo($filename, PATHINFO_EXTENSION), $allowed_extensions)) {
    // Access the filesystem
    $file_contents = file_get_contents('/path/to/files/' . $filename);
} else {
    echo 'Invalid file extension';
}