【问题标题】:upload multiple files to php server using curl command line使用 curl 命令行将多个文件上传到 php 服务器
【发布时间】:2012-07-20 22:43:09
【问题描述】:

我需要使用 curl 命令行实用程序将多个文件上传到服务器。对于单个文件,我使用没有问题:

curl -F "image=@file1.gif"   http://localhost:8888/web/Upload.php

如何处理多个文件,以便 php 变量 $_FILES["image"]["error"] 返回一个数组?

我试过了

curl -F "image=@file1.gif" -F "image=@file2.gif"  http://localhost:8888/web/Upload.php
curl -F "image=@file1.gif,image=@file2.gif"  http://localhost:8888/web/Upload.php

但这些只是在黑暗中刺伤。

【问题讨论】:

    标签: php curl


    【解决方案1】:

    诀窍是将文件上传参数命名为唯一。

    curl -F "image=@file1.gif" -F "image2=@file2.gif"  http://localhost:8888/web/Upload.php
    

    这将在$_FILES 超全局中显示为$_FILES['image']$_FILES['image2']

    要将文件分组在一个$_FILES 索引下,您需要将参数命名为数组:

    curl -F "image[]=@file1.gif" -F "image[]=@file2.gif"  http://localhost:8888/web/Upload.php
    

    【讨论】:

    • 实际上我看到了以下页面php.net/manual/en/features.file-upload.multiple.php,它表明您可以将图片作为数组上传[]["error"]。我想知道 curl 是否有可能
    • 这仅用于测试将由 iphone 应用程序使用的 Web 服务。它会一次加载许多图像
    • 哦,如果您将参数命名为数组,则可以实现巫婆卷曲。更新示例。
    【解决方案2】:

    您可以使用curl_multi_init()。我正在做一个项目,由于我是新手,可能甚至不到一年,这对我来说太难了。我需要使用 api (anonfiles.com) 上传到服务器

    注意:我的脚本基于用户输入的多个文件。代码如下。

    <?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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 2019-01-25
      • 2013-07-15
      相关资源
      最近更新 更多