Can anyone give me an example for PHP's CURLFile class?

前端 未结 5 1915
小鲜肉
小鲜肉 2020-12-02 11:16

I had a very simple PHP code to upload a file to a remote server; the way I was doing it (as has been suggested here in some other solutions) is to use cUrl to upload the fi

5条回答
  •  没有蜡笔的小新
    2020-12-02 11:48

    Php POST request send multiple files with curl function:

     'Alexia', 'address' => 'Usa', 'age' => 21, 'file' => '@'.$file1);    
        // $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[0]' => '@'.$file1, 'file[1]' => '@'.$file2);
        // CurlFile method   
        $f1 = new CurlFile($file1, mime_content_type($file1), basename($file1)); 
        $f2 = new CurlFile($file2, mime_content_type($file2), basename($file2)); 
        $data = array('name' => 'Alexia', 'address' => 'Usa', 'age' => 21, 'file[1]' => $f1, 'file[2]' => $f2);
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://url.x/upload.php');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // !!!! required as of PHP 5.6.0 for files !!!
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 1, 2
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $res2 = curl_exec($ch);
        echo $res2;
    ?>
    
    
    

提交回复
热议问题