1. Use the default curl Method
$ File = realpath ('gif/1.gif '); // the file to be uploaded $ fields ['F'] = '@'. $ file; $ CH = curl_init (); curl_setopt ($ ch, curlopt_url, "http: // localhost/ajax_server.php? Id = 1 "); curl_setopt ($ ch, curlopt_post, 1); curl_setopt ($ ch, curlopt_postfields, $ fields); curl_exec ($ ch); curl_close ($ ch );
2. Alternative Approach: Sometimes we need to upload dynamically generated content to a 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.
$ Contents = <'text' the content-Disposition: Form-data; name = "userfile"; filename = "file_name" Content-Type: in this format, I only implement the file name section. I do not know how to implement the document type. In this way, the value of Type curl_upload_server.php <XMP> <? Phpprint_r ($ _ files); echo "File Content: \ n"; $ P = Current ($ _ files); readfile ($ P ['tmp _ name']); text; $ fields ['f "; filename =" x. x'] = $ contents; // is this join key very strange? $ CH = curl_init (); curl_setopt ($ ch, curlopt_url, "http: // localhost/curl_upload_server.php"); curl_setopt ($ ch, curlopt_post, 1); curl_setopt ($ ch, curlopt_postfields, $ fields); curl_setopt ($ ch, curlopt_returntransfer, 1); $ S = curl_exec ($ ch); curl_close ($ ch); echo $ S;
Interesting
Well, solved it.
$ Contents = <'text' the content-Disposition: Form-data; name = "userfile"; filename = "file_name" Content-Type: in this format, I only implement the file name section. I do not know how to implement the document type. In this way, the value of Type curl_upload_server.php <XMP> <? Phpprint_r ($ _ files); echo "File Content: \ n"; $ P = Current ($ _ files); readfile ($ P ['tmp _ name']); text; $ fields ['f "; filename =" x. x'] = $ contents; // is this join key very strange? $ CH = curl_init (); curl_setopt ($ ch, curlopt_url, "http: // localhost/curl_upload_server.php"); curl_setopt ($ ch, curlopt_post, 1); curl_setopt ($ ch, curlopt_postfields, $ fields); curl_setopt ($ ch, curlopt_returntransfer, 1); $ S = curl_exec ($ ch); curl_close ($ ch); echo $ S;