PHP Curl upload File Code instance

Source: Internet
Author: User
Tags curl curl upload file php file

This article mainly introduced the PHP Curl upload file Code example, this article has given two kinds of realization method, and has given the realization code separately, needs the friend to be possible to refer to under

Assume server-side upload file processing script upload.php:

The code is as follows:

  

Print_r ($_post);

Print_r ($_files);

1, using the CURL default method

Copy code code as follows:

If the PHP file is UTF8 encoded, the system is GBK encoded, then you need to turn down the code, otherwise PHP can not find this file in the system

$file = Realpath (mb_convert_encoding) (' Test picture. JPG ', ' GBK ', ' UTF8 ');

$file = Realpath (' temp.jpg '); Files to upload

$fields [' f '] = ' @ '. $file; Front plus @ sign upload picture

$ch =curl_init ();

curl_setopt ($ch, Curlopt_url, ' http://localhost/upload.php ');

curl_setopt ($ch, curlopt_post,true);

curl_setopt ($ch, Curlopt_postfields, $fields);

curl_setopt ($ch, curlopt_returntransfer,true);

$content = curl_exec ($ch);

Echo $content;

2, alternative approach, sometimes we need to be dynamically generated content as files uploaded to the remote server, but do not want to build temporary files on the local server. That's the alternative.

The code is as follows:

$contents =<<< ' TEXT '

Here is the file content, also can be the picture binary, the picture needs to modify the upload file type

TEXT;

$varname = ' my '//key uploaded to the $_files array

$name = ' 3.txt ';//File name

$type = ' text/plain ';//File type

$key = "$varname"; Filename= "$namernContent-type: $typern";

$fields [$key] = $contents;

$ch =curl_init ();

curl_setopt ($ch, Curlopt_url, ' http://localhost/upload.php ');

curl_setopt ($ch, curlopt_post,true);

curl_setopt ($ch, Curlopt_postfields, $fields);

curl_setopt ($ch, curlopt_returntransfer,true);

$content = curl_exec ($ch);

Echo $content;

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.