PHP CURL File Upload – A Simple Example

Welcome to a quick tutorial on how to upload a file with PHP CURL. Need to do a server-to-server file transfer with PHP? Yes, it is definitely possible.

To do a file upload with PHP CURL, simply set CURL to do a POST and attach the file.

  • $cf = new CURLFile("FILE-TO-UPLOAD.EXT");
  • $ch = curl_init();
  • curl_setopt($ch, CURLOPT_URL, "HTTP://SITE.COM/");
  • curl_setopt($ch, CURLOPT_POST, true);
  • curl_setopt($ch, CURLOPT_POSTFIELDS, ["upload" => $cf]);
  • curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  • $result = curl_exec($ch);
  • curl_close($ch);

That covers the basics, but let us walk through a simple example in this guide – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes CURL File Upload Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

CURL FILE UPLOAD

All right, let us now get into the example on how to upload a file using PHP CURL.

 

SERVER A) FILE UPLOAD WITH CURL

1-upload.php
<?php
// SERVER A - UPLOAD FILE VIA CURL POST
// (A) SETTINGS
$url = "http://localhost/2-receive.php"; // Where to upload file to
$file = __DIR__ . DIRECTORY_SEPARATOR . "README.txt"; // File to upload
$upname = "uploaded.txt"; // File name to be uploaded as

// (B) NEW CURL FILE
$cf = new CURLFile($file, mime_content_type($file), $upname);

// (C) CURL INIT
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
  // ATTACH FILE UPLOAD
  "upload" => $cf,
  // OPTIONAL - APPEND MORE POST DATA
  "KEY" => "VALUE"
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// (D) CURL RUN
// (D1) GO!
$result = curl_exec($ch);

// (D2) CURL ERROR
if (curl_errno($ch)) {
  echo "CURL ERROR - " . curl_error($ch);
}

// (D3) CURL OK - DO YOUR "POST UPLOAD" HERE
else {
  // $info = curl_getinfo($ch);
  // print_r($info);
  echo $result;
}

// (D4) DONE
curl_close($ch);

This is pretty much a fancy version of the snippet in the introduction.

  1. First, we start by defining some settings. Where to upload the file to, which file to upload, and what file name to upload as.
  2. Create a new CURL file object.
  3. Initiate the CURL request, attach the CURL file.
  4. Run the CURL, deal with the post file upload.

 

 

SERVER B) HANDLE THE FILE UPLOAD

2-receive.php
<?php
// SERVER B - RECEIVE FILE UPLOAD
echo "SERVER B FILE UPLOAD - ";
echo move_uploaded_file($_FILES['upload']['tmp_name'], $_FILES['upload']['name'])
? "OK" : "ERROR" ;

Yep, if you need to deal with the upload on the other server – Just deal with it as a “normal file upload”.

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

 

INFOGRAPHIC CHEAT SHEET

PHP CURL File Upload (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

1 thought on “PHP CURL File Upload – A Simple Example”

Leave a Comment

Your email address will not be published. Required fields are marked *