How to upload file using cURL in PHP

Uploading files (images, documents, etc.) on web pages is an essential part of most websites.

If you are on the same server and upload file then you can easily upload it either directly using PHP or AJAX but if you want to upload it to another server then you have to use cURL.

In this tutorial, I show how you can upload file using cURL in PHP.

How to upload file using cURL in PHP


  1. Create Api to upload file
  2. File upload using cURL
  3. Output
  4. Conclusion

1. Create Api to upload file

Create api.php file.

If $_FILES['file']['name'] is isset() then upload the file to upload folder.

Assign $status and $msg value based on the upload status. Read extra data num1 passed using $_POST.

Return $response Array in JSON format.

Completed Code


$status = 0; $msg = "";

    // file name
    $filename = $_FILES['file']['name'];

    // Location
    $location = 'upload/'.$filename;

    // file extension
    $file_extension = pathinfo($location, PATHINFO_EXTENSION);
    $file_extension = strtolower($file_extension);

    // Valid extensions
    $valid_ext = array("pdf","doc","docx","jpg","png","jpeg");

    $status = 0;
        // Upload file
             $status = 1;
             $msg = "Upload successfully";
        $status = 0;
        $msg = "Invalid file extension";


$response = array(
    'status' => $status,
    'msg' => $msg,
    'num1' => $_POST['num1']
echo json_encode($response);

2. File upload using cURL

Create index.php file.

Create <form > and add file element and a submit button.

cURL file upload

If submit button is POST then upload the file using cURL.

Create CURLFile object where pass $_FILES['file']['tmp_name'],$_FILES['file']['type'], and $_FILES['file']['name'] as parameters.

Store $cfile in $postRequest['file'] Array. Here, also storing value in $postRequest['num1'] for example purpose.

Decode the cURL response to read value.

Completed Code

<!DOCTYPE html>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>How to upload file using cURL in PHP</title>


           // Create a CURLFile object
           $cfile = curl_file_create($_FILES['file']['tmp_name'],$_FILES['file']['type'],$_FILES['file']['name']);

           $postRequest = array(
               'file' => $cfile,
               'num1' => 54

           $cURL = curl_init('http://localhost/apis/api.php');
           curl_setopt($cURL, CURLOPT_POSTFIELDS, $postRequest);
           curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

           if(curl_exec($cURL) === false){
                echo 'Curl error: ' . curl_error($cURL);
                $curlResponse = curl_exec($cURL);

                $jsonArrayResponse = json_decode($curlResponse);

                echo "<pre>";
                echo "</pre>";

   <form method="post" action="" enctype="multipart/form-data">
       <input type="file" name="file"> <br>
       <input type="submit" name="submit" value="Upload file">

3. Output

View Output

4. Conclusion

Using above script you can upload a single file using cURL and you can POST extra parameters while sending a file for upload.

If you found this tutorial helpful then don't forget to share.

Leave a Comment