doutan2111 2018-07-19 05:06
浏览 182

使用curl php发送多个文件数据(要发送的文件数量不同)

I need to send multiple files using curl php, but the issue, is the number of files to be sent is varies on the user, hence I have to use some kind of iteration to create array of files to send it using "curl_file_create".

Below is my code for sending just one file. Please feel free to ask any query.

        $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);

Addition to this question. So I have four input( FILE ) fields, and a user can select multiple files for each of those fields. So, once the user has selected the array of files something like this:

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
                )

        )

)

Now I need to know how I can send all these images using curl and since the number of files is not constant, hence there has to be some iteration to be used here. - Solved

Here is the Solution:

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

            }
        }
  • 写回答

1条回答 默认 最新

  • dongnianchou7047 2018-07-19 05:53
    关注

    You can try using something like that

    // calling uploadfile function for each image file in array

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

    //Upload function:

    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))