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;