What are the advantages and disadvantages of using fsockopen for file uploads compared to cURL in PHP?

When it comes to file uploads in PHP, using cURL is generally preferred over fsockopen due to its higher level of abstraction and easier implementation of HTTP requests. cURL provides built-in support for handling file uploads, making it simpler to send files to a server. However, fsockopen can be used for file uploads as well, but it requires more manual handling of the HTTP headers and data.

// Using cURL for file uploads
$ch = curl_init();
$fp = fopen("file_to_upload.txt", "r");

curl_setopt($ch, CURLOPT_URL, "http://example.com/upload.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@file_to_upload.txt'));

$response = curl_exec($ch);

fclose($fp);
curl_close($ch);