user1263019 how to upload a file using php curl code example
Example: user1263019 how to upload a file using php curl
<?php
function getCurlValue($filename, $contentType, $postname)
{
if (function_exists('curl_file_create')) {
return curl_file_create($filename, $contentType, $postname);
}
$value = "@{$this->filename};filename=" . $postname;
if ($contentType) {
$value .= ';type=' . $contentType;
}
return $value;
}
$filename = '/path/to/file.jpg';
$cfile = getCurlValue($filename,'image/jpeg','cattle-01.jpg');
$data = array('file' => $cfile);
$ch = curl_init();
$options = array(CURLOPT_URL => 'http://your/server/api/upload',
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$header_info = curl_getinfo($ch,CURLINFO_HEADER_OUT);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($ch);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>File Upload results</title>
</head>
<body>
<p>Raw Result: <?=$result?>
<p>Header Sent: <?=$header_info?></p>
<p>Header Received: <?=$header?></p>
<p>Body: <?=$body?></p>
</body>
</html>