curl file upload code example

Example 1: php upload file via curl

if (function_exists('curl_file_create')) { // php 5.5+
  $cFile = curl_file_create($file_name_with_full_path);
} else { // 
  $cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

Example 2: how to download a file with curl

//this downloads the file specified
curl "http://standards-oui.ieee.org/oui/oui.txt" --output macvendors.txt

Example 3: curl upload file

curl -F "FIELDNAME=@FILE" http://URL 
# FIELDNAME can be whatever you want, it is the key value of the form field
# the @ is important to point to a local file

Example 4: linux curl upload file ftp

# curl -T backup-file.tar.gz ftp://username:[email protected]/backup/

Tags:

Php Example