Multiple file downloads using cURL

I use cURL to transfer image files from one server to another using PHP. This is my cURL code:

// Transfer the original image and thumbnail to our storage server
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://' . $server_data['hostname'] . '.localhost/transfer.php');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    'upload[]' => '@' . $tmp_uploads . $filename,
    'upload[]' => '@' . $tmp_uploads . $thumbname,
    'salt' => 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$resp = curl_exec($ch);

This is the code in the transfer.php file on the server that I upload:

if($_FILES && $_POST['salt'] == 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q')
{
    // Save the files
    foreach($_FILES['upload']['error'] as $key => $error)
    {
        if ($error == UPLOAD_ERR_OK)
        {
            move_uploaded_file($_FILES['upload']['tmp_name'][$key], $_FILES['upload']['name'][$key]);
        }
    }
}

Everything seems to work, except for one small logical error. Only one file is saved on the server to which I transfer. This is probably due to the fact that I call both images upload[]in my array of message fields, but I do not know how to do this. I am trying to imitate this:

<input type="file" name="upload[]" />
<input type="file" name="upload[]" />

Does anyone know how I can make this work? Thank!

+5
source share
2 answers

here is your error in calling curl ...

var_dump($post)

$post, ...

$post = array(
    'upload[0]' => '@' . $tmp_uploads . $filename,
    'upload[1]' => '@' . $tmp_uploads . $thumbname,
    'salt' => 'q8;EmT(Vx*Aa`fkHX:up^WD^^b#<Lm:Q'
);
+3

, move(). , ( curl script). ( curl script) , . , , PHP.

move():

if (!move_uploaded_file($_FILES['upload']['tmp_name'][$key], $_FILES['upload']['name'][$key])) {
   echo "Unable to move $key/";
   echo $_FILES['upload']['tmp_name'][$key];
   echo ' to ';
   echo $_FILES['upload']['name'][$key];
}

( ).

0

All Articles