0

I am writing a PHP script that passes a file and a bunch of parameters to upload.php.

Here is a snippet of upload.php

if (!empty($_FILES["Image"])) {
    $user_id = $_POST["user_id"];
    $user_name = $_POST["user_name"];
    $file_name = $_FILES["Image"]["name"];
}

I am trying to send a curl request which passes those parameters.

function post($file) {
    $url = 'http://localhost/mc/upload.php';
    $fields = array(
                'user_id' => "test_id",
                'user_name' => "test_user",
                'Image' => '@' . $file
                );
    $fields_string = "";
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    $result = curl_exec($ch);

The file is sent as a $_POST and not in $_FILES. Also what I sent is received as a string in $_POST['Image']

I have tried using the answer given in this post here

if (function_exists('curl_file_create')) { 
                $cFile = curl_file_create($file_name_with_full_path);
    } else { 
                $cFile = '@' . realpath($file_name_with_full_path);
    }

Using the above snippet to prepare the file before sending it. Since I am using PHP version 5.5+ an object is created. It gives the following error

Object of class CURLFile could not be converted to string

This error happens when I am concatenating all the parameters to $fields_string

Hari Krishnan
  • 2,049
  • 2
  • 18
  • 29
  • Does this answer your question? [how to upload file using curl with PHP](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – Broken Arrow May 22 '21 at 14:58
  • I don't see a point why would you need "curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));"? – Broken Arrow May 22 '21 at 14:59
  • @BrokenArrow When I tried the answer that you shared, it gave me the following error `Object of class CURLFile could not be converted to string` – Hari Krishnan May 22 '21 at 15:03
  • `curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));` By removing this line I was able to pass the `$_POST` parameters. But it throws a Undefined Index error when accessing `$_FILE['Image']` – Hari Krishnan May 22 '21 at 15:08

1 Answers1

0

Uploading files with cURL will become available like just a regular HTTP FILE POST and should be available through $_FILE global variable to be handled the same regular way you'd handle a regular file upload with PHP.

test.php

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, "http://localhost/Projects/Test/test-response.php");
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

curl_setopt($cURL, CURLOPT_POSTFIELDS, [
    "ID" => "007", 
    "Name" => "James Bond", 
    "Picture" => curl_file_create(__DIR__ . "/test.png"), 
    "Thumbnail" => curl_file_create(__DIR__ . "/thumbnail.png"), 
]);

$Response = curl_exec($cURL);
$HTTPStatus = curl_getinfo($cURL, CURLINFO_HTTP_CODE);

curl_close ($cURL);

print "HTTP status: {$HTTPStatus}\n\n{$Response}";

test-response.php

print "\n\nPOST";
foreach($_POST as $Key => $Value)print "\n\t{$Key} = '{$Value}';";

print "\n\nFILES";
foreach($_FILES as $Key => $Value)print "\n\t{$Key} = '{$Value["name"]}'; Type = '{$Value["type"]}'; Temporary name = '{$Value["tmp_name"]}'";

Output

HTTP status: 200

POST
    ID = '007';
    Name = 'James Bond';

FILES
    Picture = 'test.png'; Type = 'application/octet-stream'; Temporary name = 'C:\Windows\Temp\php76B5.tmp'
    Thumbnail = 'thumbnail.png'; Type = 'application/octet-stream'; Temporary name = 'C:\Windows\Temp\php76C6.tmp'

I assume you will keep the relevant 2 images in the same path as 'test.php' to obtain the output as shown.

Broken Arrow
  • 576
  • 3
  • 12