PHP Curl Upload file Code instance,
Suppose the server-side upload file processing script upload.php:
Copy the Code code as follows:
<?php
Print_r ($_post);
Print_r ($_files);
1, using the default method of CURL
Copy the Code code as follows:
If the PHP file is UTF8 encoded, the system is GBK encoded, then you need to turn the code, or PHP can not find this file in the system
$file = Realpath (mb_convert_encoding (' Test picture. JPG ', ' GBK ', ' UTF8 ');
$file = Realpath (' temp.jpg '); The file to upload
$fields [' f '] = ' @ '. $file; Preceded by an @ symbol to upload a 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, the alternative approach , sometimes we need to be dynamically generated content as a file upload to the remote server, but do not want to build a temporary file on the local server. This is the alternative way of writing.
Copy the Code code as follows:
$contents =<<< ' TEXT '
Here is the file content, can also be the picture binary, the picture needs to modify the upload file type
TEXT;
$varname = ' my ';//upload to key in the $_files array
$name = ' 3.txt ';//File name
$type = ' text/plain ';//File type
$key = "$varname \"; Filename=\ "$name \r\ncontent-type: $type \ r \ n";
$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;
http://www.bkjia.com/PHPjc/990551.html www.bkjia.com true http://www.bkjia.com/PHPjc/990551.html techarticle php Curl Upload file Code instance, assuming the server side upload file processing script upload.php: Copy code code as follows: PHP print_r ($_post); Print_r ($_files); 1, using Curl Default party ...