【问题标题】:send multiple files data using curl php ( number of files to be sent varies )使用 curl php 发送多个文件数据(要发送的文件数量不同)
【发布时间】:2018-07-19 05:06:21
【问题描述】:

我需要使用 curl php 发送多个文件,但问题是要发送的文件数量因用户而异,因此我必须使用某种迭代来创建文件数组以使用 " curl_file_create”。

下面是我只发送一个文件的代码。如有任何疑问,请随时提出。

        $url = 'http://localhost/target_url';
        $ch = curl_init();
        // Loop over files
        $tmpfile = $_FILES['input_name']['tmp_name'];
        $filename = basename($_FILES['input_name']['name']);
        $fields_string =  array(
            'uploaded_file' => curl_file_create($tmpfile, $_FILES['input_name']['type'], $filename));

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);   
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response_json = curl_exec($ch);
        curl_close($ch);
        $response=json_decode($response_json, true);

对此问题的补充。 所以我有四个输入(文件)字段,用户可以为每个字段选择多个文件。因此,一旦用户选择了文件数组,如下所示:

Array( [x1] => Array
        (
            [name] => Array
                (
                    [0] => img1.png
                )

            [type] => Array
                (
                    [0] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\php6F66.tmp
                )

            [error] => Array
                (
                    [0] => 0
                )

            [size] => Array
                (
                    [0] => 6698
                )

        )

    [x2] => Array
        (
            [name] => Array
                (
                    [0] => img-2 - Copy.png
                    [1] => img-3.png
                    [2] => img-4.png
                )

            [type] => Array
                (
                    [0] => image/png
                    [1] => image/png
                    [2] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\php6F67.tmp
                    [1] => C:\wamp\tmp\php6F77.tmp
                    [2] => C:\wamp\tmp\php6F88.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )

            [size] => Array
                (
                    [0] => 165091
                    [1] => 165091
                    [2] => 6698
                )

        )

    [x3] => Array
        (
            [name] => Array
                (
                    [0] => img-5 - Copy.png
                    [1] => img-6.png
                    [2] => img-6.png
                )

            [type] => Array
                (
                    [0] => image/png
                    [1] => image/png
                    [2] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\php6FA8.tmp
                    [1] => C:\wamp\tmp\php6FB9.tmp
                    [2] => C:\wamp\tmp\php6FD9.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )

            [size] => Array
                (
                    [0] => 165091
                    [1] => 165091
                    [2] => 6698
                )

        )

)

现在我需要知道如何使用 curl 发送所有这些图像,并且由于文件数量不是恒定的,因此必须在此处使用一些迭代。 - 解决了

解决方法如下:

function uploadfile($tmp_value, $type_value, $imageName){
            $url = 'http://localhost/target_url';
            $ch = curl_init();

            $fields_string =  array(
                'uploaded_file' => curl_file_create($tmp_value, $type_value, $imageName));

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);   
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response_json = curl_exec($ch);
            curl_close($ch);
            $file_response=json_decode($response_json, true);
            return $response_json;
        } 


foreach ($_FILES as $images_key => $images) {
            foreach ($images['name'] as $imageData_key => $imageName){

                foreach ($images['type'] as $type_key => $type_value) {

                    foreach ($images['tmp_name'] as $tmp_key => $tmp_value) {

                    }

                }

                echo $this->uploadfile($tmp_value, $type_value, $imageName);

            }
        }

【问题讨论】:

  • 如果您想要一个好的答案,您需要分享用于上传文件的表单代码。
  • 实际上表单太大而无法理解,这本身可能会让事情变得难以理解,但我能做的只是解释多文件表单部分。让我给你发一下
  • 你应该把foreach ($images['type'] as $type_key => $type_value)改成$type_value = $images['type'][$imageData_key](和$images['tmp_name']一样)

标签: php file curl


【解决方案1】:

你可以尝试使用类似的东西

//为数组中的每个图像文件调用uploadfile函数

foreach ($imagesArray as $images_key => $images){
    foreach ($images['name'] as $imageData_key => $imageName){
       $response = uploadfile($imagesArray[$images_key][$imageData_key]['tmp_name'], $imagesArray[$images_key][$imageData_key]['type'], $imageName);
        //do something with $response below
        echo $response;
    }
}

//上传函数:

function uploadfile($tmpfile, $type, $filename){
    $url = 'http://localhost/target_url';
    $ch = curl_init();
    // Loop over files
    $tmpfile = $_FILES['input_name']['tmp_name'];
    $filename = basename($_FILES['input_name']['name']);
    $fields_string =  array(
        'uploaded_file' => curl_file_create($tmpfile, $type, $filename));

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);   
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response_json = curl_exec($ch);
    curl_close($ch);
    $response=json_decode($response_json, true);
    return $response;
}

【讨论】:

  • 您的解决方案不是重点,但它确实给了我一个解决问题的想法。无论如何,我正在用答案更新查询。
猜你喜欢
  • 2018-03-09
  • 2016-06-14
  • 1970-01-01
  • 2012-06-19
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
相关资源
最近更新 更多