POST buffer to file using libcurl in C++

  Kiến thức lập trình
========= upload.php ============
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

I upload a file to the server as so:

$ curl http://127.0.0.1/upload.php -F "fileToUpload=@/home/user/file.txt"

I want to upload the buffer below to a file on the server. I do this with FTP but in this case, I want to be able to use SSL

char *data = "this is the data to upload";

My code in my C++ program is below but I’m not sure how to structure the POSTFIELDS correctly. I see other threads on POSTing such as using this code:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

but they don’t seem to be using any specific fields or even using a php script. Is there a way to do it without php?

 curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1/upload.php");
 /* Now specify the POST data */
 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "fileToUpload=file.txt", data);

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT