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

前端 未结 5 1896
小鲜肉
小鲜肉 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:49

    There is a snippet on the RFC for the code: https://wiki.php.net/rfc/curl-file-upload

    curl_setopt($curl_handle, CURLOPT_POST, 1);
    $args['file'] = new CurlFile('filename.png', 'image/png', 'filename.png');
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
    

    You can also use the seemingly pointless function curl_file_create( string $filename [, string $mimetype [, string $postname ]] ) if you have a phobia of creating objects.

    curl_setopt($curl_handle, CURLOPT_POST, 1);
    $args['file'] = curl_file_create('filename.png', 'image/png', 'filename.png');
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
    

提交回复
热议问题