What potential pitfalls should be considered when attempting to delete folders and files in PHP?

When attempting to delete folders and files in PHP, it is important to consider potential pitfalls such as file permissions, file existence checks, and error handling. Make sure that the PHP script has the necessary permissions to delete the files and folders, check if the file or folder exists before attempting to delete it, and implement error handling to gracefully handle any issues that may arise during the deletion process.

$filePath = 'path/to/file.txt';

// Check if file exists before attempting to delete
if (file_exists($filePath)) {
    // Check file permissions before attempting to delete
    if (is_writable($filePath)) {
        // Attempt to delete the file
        if (unlink($filePath)) {
            echo 'File deleted successfully';
        } else {
            echo 'Error deleting file';
        }
    } else {
        echo 'File is not writable';
    }
} else {
    echo 'File does not exist';
}