how to upload file using curl with PHP

To upload a file using cURL in PHP, you can use the curl_setopt function to specify the CURLOPT_POSTFIELDS option with the file data.

Here's an example of how you can do this:

<?php

// Open a file
$file = fopen("file.txt", "r");

// Read the file into a variable
$data = fread($file, filesize("file.txt"));

// Close the file
fclose($file);

// Set up cURL
$ch = curl_init();

// Set the URL
curl_setopt($ch, CURLOPT_URL, "http://example.com/upload");

// Set the HTTP method to POST
curl_setopt($ch, CURLOPT_POST, true);

// Set the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Execute the cURL request
curl_exec($ch);

// Close the cURL handle
curl_close($ch);

?>

Watch a course Learn object oriented PHP

This will send a POST request to http://example.com/upload with the contents of the file.txt file as the POST data.