简体   繁体   中英

Upload files with PHP curl

For our customer, I have to make a mail backup system that backups everything sent (subject, body, attachments, headers, ...)

But i'm stuck with the file uploads via curl.

This is my code so far:

$filesBackup = array('api_key' => 'xxx', 'files' => array());

                    foreach ($attachment as $att) {

                        $filesBackup['files'][] = new \CURLFile($att[0]);


                    $request = curl_init('http://xxx/webhooks/uploadtos3.php');
                    curl_setopt($request, CURLOPT_POST, true);
                    curl_setopt($request, CURLOPT_SAFE_UPLOAD, false);
                    curl_setopt(
                        $request,
                        CURLOPT_POSTFIELDS, http_build_query($filesBackup));
                    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
                    curl_exec($request);
                    curl_close($request);

The problem here is that the filepath ( $att[0] ) is the following: " http://xxx/uploads/zonnepanelen/file.pdf "

So the only thing I receive at the uploadtos3.php is this array:

array(3) {
      ["name"]=>
      string(89) "http://xxx/uploads/zonnepanelen/file.pdf"
      ["mime"]=>
      string(0) ""
      ["postname"]=>
      string(0) ""
    }

Is there anyone who can help me with this?

Many thanks in advance!

update code with below one

$filesBackup['files'][]  should contain below keys?

array(
      'file' =>
          '@'            . $_FILES['file']['tmp_name']
          . ';filename=' . $_FILES['file']['name']
          . ';type='     . $_FILES['file']['type']
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM