Phpcurl uploads the file code instance ,. Phpcurl uploads the file code instance. assume that the server uploads the file processing script upload. php: Copy the code as follows: phpprint_r ($ _ POST); print_r ($ _ FILES); 1. use the default php CURL of curl to upload the file code instance,
Assume that the server uploads the file processing script upload. php:
The code is as follows:
<? Php
Print_r ($ _ POST );
Print_r ($ _ FILES );
1. use the default CURL method
The code is as follows:
// If the Php file is utf8 encoded and the system is GBK encoded, it needs to be converted to the next encoding. otherwise, Php cannot find this file in the system.
$ File = realpath (mb_convert_encoding ('test Image. JPG ', 'gbk', 'utf8 '));
$ File = realpath('temp.jpg '); // the object to be uploaded
$ Fields ['F'] = '@'. $ file; // add a @ symbol to upload an image.
$ 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 practicesSometimes we need to upload the dynamically generated content to the remote server as a file, but we do not want to build temporary files on the local server. In this way, this alternative writing method is available.
The code is as follows:
$ Contents = <'text'
This is the file content, or the image binary. you need to modify the Upload file type for the image.
TEXT;
$ Varname = 'my'; // key uploaded to 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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/990551.htmlTechArticlephp curl upload file code instance, assuming the server side upload file processing script upload. php: the code is as follows: php print_r ($ _ POST); print_r ($ _ FILES); 1. use the default side of CURL...