如何使用Php Curl将$ _FILES传递给远程服务器以及少量$ _POST数据

时间:2015-01-24 13:41:55

标签: php post curl

以下是从客户端服务器向远程服务器发送数据的代码:

$fields_string = "";
        $fields = array();
        $file_name_with_full_path = realpath('./sample.jpeg');
        $url = 'http://192.168.1.xx/proj_v6/upload_amax_data';
        if (isset($_FILES['files']['tmp_name'][0])) {
            $filename  = $_FILES['files']['tmp_name'][0];
            $handle    = fopen($filename, "r");
            $data      = fread($handle, filesize($filename));
            $fields = array(
                          'file' =>
          '@'            . $_FILES['files']['tmp_name'][0]
          . ';filename=' . $_FILES['files']['name'][0]
          . ';type='     . $_FILES['files']['type'][0]
                        );

        }
        $fields = array(
            'r_token' => urlencode(REMOTE_AUTH_TOKEN),
            'lname' => urlencode('$lname'),
            'fname' => urlencode('$first_name'),
            'title' => urlencode('$title'),
            'company' => urlencode('$institution'),
            'age' => urlencode('$age'),
            'email' => urlencode('$email'),
            'phone' => urlencode('$phone')
        );
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        rtrim($fields_string, '&');
        //open connection
        $ch = curl_init();
        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        //execute post
        $result = curl_exec($ch);
        //close connection
        curl_close($ch);

我在远程服务器上获取$ _POST数据但不是$ _FILES数据...尝试了一些网站但没有运气......

重要提示:我如何通过" $ _ FILES"以及" $ _ POST"使用php curl到远程服务器的数据。

1 个答案:

答案 0 :(得分:0)

if (isset($_FILES['files']['tmp_name'][0])) {
            $filename  = $_FILES['files']['tmp_name'][0];
            $handle    = fopen($filename, "r");
            $data      = fread($handle, filesize($filename));
            $fields['image'] = '@'.$_FILES['files']['tmp_name'][0]
          . ';filename=' . $_FILES['files']['name'][0]
          . ';type='     . $_FILES['files']['type'][0];
        }