Friday, 21 June 2013

Uploading files with CURL in PHP the right way.

Uploading files with CURL in PHP use to involve prefixing the file path with an @ using a POST param.  But recently PHP has added curl_file_create() see: https://wiki.php.net/rfc/curl-file-upload

With this you can now upload a file as easily as:

$params = ['file_key' => curl_file_create('path/to/file')];
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);


PHP takes another step in the right direction ;-)