Upload a File via POST with cURL and PHP 7

Posted on August 29, 2017 at 2:53 pm

Example of sending or uploading a file via POST with cURL and PHP 7:

File content of php-send-file-curl.php:

<?php
if ( isset($_POST['submitfile']) )
{
    // Make sure there are no upload errors
    if ($_FILES['upfile']['error'] > 0)
    {
        die("Error uploading file...");
    }
 
    // Prepare the cURL file to upload, including file name and MIME type
    $post = array(
    'upfile' => new CurlFile($_FILES["upfile"]["tmp_name"], $_FILES["upfile"]["type"], $_FILES["upfile"]["name"]),
    );
 
    // Include the other $_POST fields from the form?
    $post = array_merge($post, $_POST);
 
    // Prepare the cURL call to upload the external script
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.website.com/php-receive-file.php");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $result = curl_exec($ch);
    curl_close($ch);
 
    // Print the result?
    print_r($result);
}
?>
 
<form action="php-send-file-curl.php" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="upfile">
    <input type="hidden" name="submitfile" value="1">
    <input type="submit" value="Upload File">
</form>

File content of php-receive-file.php:

<?php
 
if ( isset($_POST['submitfile']) )
{
    print_r($_POST);
 
    print_r($_FILES);
}
 
?>

Example output:

Array
(
    [submitfile] => 1
)
Array
(
    [upfile] => Array
        (
            [name] => 1234.mp3
            [type] => audio/mpeg
            [tmp_name] => /tmp/phpxoOOl1
            [error] => 0
            [size] => 14304
        )
 
)

To upload multiple files, do it like this:

for($i=0; $i<count($_FILES['upfile']['tmp_name']); $i++) 
    $post['upfile['.$i.']'] = new CurlFile($_FILES["upfile"]["tmp_name"][$i], $_FILES["upfile"]["type"][$i], $_FILES["upfile"]["name"][$i]);

Example output:

Array
(
    [upfile] => Array
        (
            [name] => Array
                (
                    [0] => pdf-sample.pdf
                    [1] => pdf-sample.pdf
                )
 
            [type] => Array
                (
                    [0] => application/pdf
                    [1] => application/pdf
                )
 
            [tmp_name] => Array
                (
                    [0] => /tmp/phpGMjMsd
                    [1] => /tmp/php7udG4F
                )
 
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )
 
            [size] => Array
                (
                    [0] => 7945
                    [1] => 7945
                )
 
        )
 
)

More information:
PHP: rfc:curl-file-upload
PHP: curl_file_create – Manual

Updated on May 15, 2020 at 2:14 pm

Receive updates via email

Other Posts

Updated Posts