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:


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

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

// Close the file

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

// Set the URL
curl_setopt($ch, CURLOPT_URL, "");

// 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

// Close the cURL handle


Watch a course Learn object oriented PHP

This will send a POST request to with the contents of the file.txt file as the POST data.