What common security issues can arise when handling file operations in PHP, as seen in the provided code snippet?

One common security issue when handling file operations in PHP is the lack of input validation, which can lead to directory traversal attacks. To prevent this, always sanitize user input and ensure that file paths are properly validated before performing any file operations.

// Sanitize user input to prevent directory traversal attacks
$filename = basename($_GET['filename']);
$filepath = '/path/to/files/' . $filename;

// Validate file path before performing any file operations
if (file_exists($filepath)) {
    // Perform file operations here
    // For example, reading the file contents
    $file_contents = file_get_contents($filepath);
    echo $file_contents;
} else {
    echo 'File not found';
}