0

Please tell me how to transfer a file using curl post without php warning that $ data_curl is an array PHP Notice: Array to string conversion...

$data_curl = array('domain' => $domain, 'token' => '*', 'files' => array());
foreach ($_FILES["files"]["tmp_name"] as $file) {
   $tmpfile = $_FILES['files']['tmp_name'][$i];
   $filename = $name_file."_".time().".".$type;
   $data_curl = $data_curl + array(
      'file'.$i => curl_file_create($tmpfile, $_FILES['files']['type'][$i], $filename)
   );
   $i++;    
} 
$ch = curl_init('https://google.com');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_curl);
curl_exec($ch);

json_encode and http_build_query do not send file.

9
  • Change content-type to multipart/form-data Commented Apr 5, 2021 at 15:08
  • 1
    Learned something new then. If only we had the ACTUAL error message instead of a cropped one Commented Apr 5, 2021 at 15:13
  • @hppycoder I agree. The OP needs to show on which line the error is coming and what's inside those inputs from the user. Commented Apr 5, 2021 at 15:16
  • @hppycoder error in this line curl_setopt($ch, CURLOPT_POSTFIELDS, $data_curl); Commented Apr 5, 2021 at 15:18
  • @Vector Did the header change fix the issue? Commented Apr 5, 2021 at 15:22

1 Answer 1

1

The reason why error pops up is because you have

$data_curl = array('files' => array());

The values cannot be arrays in themselves and rather just a string or an integer or floats or booleans with an exception of CURLFile objects .

Also, there is no point in sending files key as a POST param with no value inside it. So you could better remove it completely and just keep $data_curl = array();

Sign up to request clarification or add additional context in comments.

4 Comments

How then can I pass some variables in the array?
@Vector Can you edit your post with an example?
@Vector Why do you still need 'files' => array() at the end?
Without 'files' => array () at the end works without warnings. Thanks! The question is closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.