Recently in writing a PHP send log function, need a background upload function, need to send a POST request with curl, but the network to find some articles, tested without any reaction, the following is my actual test through the upload code:
/**
* Curl File Upload
* @var struing $r _file and file name for uploading files
*/
function Upload_file ($r _file)
{
/* $fields [' uploadfile '] = "@". Realpath ($r _file);
$varname = ' UploadFile ';
$name = $r _file;
$type = ' Text/plain ';
$key = "$varname \"; Filename=\ "$name \r\ncontent-type: $type \r\naccept: \" ";
$fields [$key] = file_get_contents ($r _file);
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url,remote_host. /source/uploadlog.php ');
curl_setopt ($curl, curlopt_post,1);
curl_setopt ($curl, Curlopt_postfields, $fields);
curl_setopt ($curl, Curlopt_returntransfer, 1);
curl_setopt ($curl, Curlopt_header, 0);
$result = curl_exec ($curl); $result Get page Information
Curl_close ($curl);
echo $result;
$json _data = Json_decode ($result, true);
if ($json _data["status"] = = 1)
{
return true;
}
return false;
}
PHP Curl Post Upload file