32

I need to upload multiple files to a server using the curl command line utility. for a single file I have no problem using:

curl -F "[email protected]"   http://localhost:8888/web/Upload.php

how would I do this with more than one file so that the php variable $_FILES["image"]["error"] would return an array?

I've tried

curl -F "[email protected]" -F "[email protected]"  http://localhost:8888/web/Upload.php
curl -F "[email protected],[email protected]"  http://localhost:8888/web/Upload.php

but these are a stab in the dark.

2 Answers 2

62

The trick is to name the file uploading parameters unique.

curl -F "[email protected]" -F "[email protected]"  http://localhost:8888/web/Upload.php

This will show up in the $_FILES superglobal as $_FILES['image'] and $_FILES['image2'].

To make the files grouped under one $_FILES index you need to name the parameters as arrays:

curl -F "image[][email protected]" -F "image[][email protected]"  http://localhost:8888/web/Upload.php
Sign up to request clarification or add additional context in comments.

4 Comments

Actually I'd seen the following page php.net/manual/en/features.file-upload.multiple.php which showed you could upload as an array pictures[]["error"]. i was wondering if that was possible from curl
this is just for testing a web service which will be used by an iphone app. it will load a numer of images at once
Oh, that can be achieved witch curl if you name the params as arrays. Updated example.
thank you so much for showing me how to name the params as array. Foolish little me was trying to do param=[...]
-1

You can use curl_multi_init(). I was working on a project, and since I'm a newbie, not even a year probably, it was terrible hard for me. I needed to upload to a server using an api (anonfiles.com)

NOTE: My script is based on multiple files input from users. The code is as follows.

<?php
if (isset($_POST['btnUpload'])){
    $total = count($_FILES['file']['name']);
    for( $i=0 ; $i < $total ; $i++ ) {
      $filename = $_FILES['file']['name'][$i];
      $filedata = $_FILES['file']['tmp_name'][$i];
      $filetype = $_FILES['file']['type'][$i];
      $chfile = new CURLFile($filedata, $filetype, $filename); //creating a CURL object
      $request[]=['file'=>$chfile]; //making an asscoiative array with [1]=>['file'=>'curlobject'] and so on
      $urls[] = "https://api.anonfiles.com/upload?token=ced2abXXXXXXX";//added this each time because this was the only one url
    }

    print_r($request);//just for verification
    echo "<br>";
    print_r($urls);//verification
    echo "<br>";
    echo "<br>";
    $mh = curl_multi_init(); //initialise multi curl
    foreach ($urls as $key => $url) {
        $chs[$key] = curl_init($url);
        //set your own opts, these are the minimum required
        curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
        curl_setopt($chs[$key], CURLOPT_POST, true);
        curl_setopt($chs[$key], CURLOPT_POSTFIELDS, $request[$key]);//adding file data
        curl_setopt($chs[$key], CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));//make sure this header is there for it to act like data


        curl_multi_add_handle($mh, $chs[$key]);//add them to the handle that we initialised
    }

    //running the requests
    $running = null;
    do {
      curl_multi_exec($mh, $running);
    } while ($running);

    //getting the responses
    $responsearray = array();//Just to store the responses
    foreach(array_keys($chs) as $key){
        $error = curl_error($chs[$key]);
        $last_effective_URL = curl_getinfo($chs[$key], CURLINFO_EFFECTIVE_URL); 
        $time = curl_getinfo($chs[$key], CURLINFO_TOTAL_TIME);//response time
        $response = curl_multi_getcontent($chs[$key]);  // get results
        array_push($responsearray, $response);
        curl_multi_remove_handle($mh, $chs[$key]);//removing it as a handle
    }
    // close current handler
    curl_multi_close($mh);
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    print_r($responsearray);
}

Comments

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.