Upload files through curl in PHP

Source: Internet
Author: User

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;
Related Article

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.