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


Contents

  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

<?php

$status = 0; $msg = "";

if(isset($_FILES['file']['name'])){
    // 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;
    if(in_array($file_extension,$valid_ext)){
        // Upload file
        if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
             $status = 1;
             $msg = "Upload successfully";
        } 
    }else{
        $status = 0;
        $msg = "Invalid file extension";
    }

}

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

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>
<html>
<head>
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <title>How to upload file using cURL in PHP</title>
</head>
<body>
   <?php

   if(isset($_POST['submit'])){

       if(isset($_FILES['file']['name'])){
           // 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);
           }else{
                $curlResponse = curl_exec($cURL);

                $jsonArrayResponse = json_decode($curlResponse);

                echo "<pre>";
                print_r($jsonArrayResponse);
                echo "</pre>";
           }
           curl_close($cURL);
       }

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

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