PHP Curl Post Upload file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.