200 asked for help on how to set HTTPHEADER to upload files!

Source: Internet
Author: User
Tags http file upload
200 asked for help on how to set HTTPHEADER to upload files! Which of the following heroes has the experience of using CURL to set HTTPHEADER to upload files?

Please advise

PS: not '@'. file name, but Content-Type: application/octet-stream


Reply to discussion (solution)

Curl does not support this method. you need to construct your own data packets. I have studied

Curl does not support this method. you need to construct your own data packets. I have studied

Is there an example?

Http://cn.php.net/fsockopen
CTRL + F search for boundary. let's take a good look at the example and build an http request header for file upload. it is reasonable to say that CURL should build the same request header.

Summarize several methods of curl used in the project
1. default call method of php curl, access url in get mode
Java code
....
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // sets the http header
Curl_setopt ($ ch, CURLOPT_ENCODING, "gzip"); // Set the client to support gzip compression
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 30); // Set the connection wait time
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_exec ($ ch );
If ($ error = curl_error ($ ch )){
// Handle errors
Return-1;
}
Fclose ($ fp );

$ Curl_code = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); // Get the http return value
If ($ curl_code = 200 ){
// Normal access url
}
// Exception
....

....
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // sets the http header
Curl_setopt ($ ch, CURLOPT_ENCODING, "gzip"); // Set the client to support gzip compression
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 30); // Set the connection wait time
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_exec ($ ch );
If ($ error = curl_error ($ ch )){
// Handle errors
Return-1;
}
Fclose ($ fp );

$ Curl_code = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); // Get the http return value
If ($ curl_code = 200 ){
// Normal access url
}
// Exception
....
2. set http header to support curl access to the lighttpd server
Java code
$ Header [] = 'CT :';

$ Header [] = 'CT :';
3. set curl to get only the http header and not the body:
Java code
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_NOBODY, 1 );

Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_NOBODY, 1 );
Or you can only get the body:
Java code
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // make sure we get the body
Curl_setopt ($ ch, CURLOPT_NOBODY, 0 );

Curl_setopt ($ ch, CURLOPT_HEADER, 0); // make sure we get the body
Curl_setopt ($ ch, CURLOPT_NOBODY, 0 );
4. to access the VM, you must set the Host
$ Header [] = 'host: '. $ Host;
5. Use post, put, delete and other REStful access URLs
Post:
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Put, delete:
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "DELETE"); // or PUT, which must be supported by the server.
6. Save the downloaded content as an object
Curl_setopt ($ ch, CURLOPT_FILE, $ fp );

Summarize several methods of curl used in the project
1. default call method of php curl, access url in get mode
Java code
....
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // sets the http header
Curl_setopt ($ ch, CURLOPT_ENCO ......

It seems that CURLOPT_HTTPHEADER is not used.

Http://cn.php.net/fsockopen
CTRL + F search for boundary. let's take a good look at the example and build an http request header for file upload. it is reasonable to say that CURL should build the same request header.

Thank you. does CURL support this Upload method?

It may be complicated, so curl provides the '@'. file name method. Leave the work of constructing headers and data to yourself

However, curl still provides CURLOPT_UPLOAD to upload files, but actually uses the PUT request
Only suitable for uploading files to ftp
In php, you need to read data from php: // input.

Using the code provided by #5 produces an unrecognized request. if you comment out the file upload section, this error is still reported. I don't know why
However, there seems to be no difference between curl PUT-based Datagram.

Now, you can write 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 cannot be obtained after the upload.
 
  
&lt;? 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; &lt;/pre&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; as a result, &lt;pre class = "sycode" name = "code"&gt; $ varname = 'My '; $ name = '3.txt '; $ type = 'text/plain'; $ key = &lt;text $ varname "; filename =" $ nameContent-Type: $ typeTEXT; $ fields [$ key] = $ content S; &lt;/pre&gt; &lt;br/&gt; Note: I switched \ n to \ r \ n in Windows. &lt;br/&gt; No way, sun in the early years is a fight for MS, not now? &lt;Br/&gt; I always thought that the uploaded file type was identified by php, but it was originally provided by the browser &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; as a result, PHP code is of type &lt;br/&gt; $ varname = 'my '; &lt;br/&gt; $ name = '3.txt '; &lt;br/&gt; $ type = 'text/plain '; &lt;br/&gt; $ key = &lt;TEXT &lt;br/&gt; $ varname "; filename =" $ name &lt;br/&gt; Content-Type: $ type &lt;br/&gt; TEXT; &lt;br/&gt; $ fields [$ key] = $ contents; &lt;br/&gt; Note: I am in Windows, linux ...... &lt;Br/&gt; Thank you very much. Is there a complete POST example of CURL uploading by splicing HTTPHEADER information? &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; #10 is &lt;br/&gt; curl_upload_server.php is the testing server. &lt;br/&gt; &lt;xmp&gt; &lt;br/&gt; &lt;? Php &lt;br/&gt; print_r ($ _ FILES); &lt;br/&gt; echo "file content: \ n "; &lt;br/&gt; $ p = current ($ _ FILES); &lt;br/&gt; readfile ($ p ['tmp _ name']); &lt;br/&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; &lt;p class = "modified_message"&gt; the complete code of this post was edited by xuzuning at 13:19:42, 2012-04-10 &lt;/p&gt; &lt;pre class = "sycode" name = "code"&gt; $ contents = &lt;'text' in the datagram should be Content-Disposition: form-data; name = "userfile"; filename = "file_name" Content-Type: the format of file Content is as follows: Server End code curl_upload_server.php &lt;xmp&gt; &lt;? Phpprint_r ($ _ FILES); // Check the upload information echo "file content: \ n"; $ p = current ($ _ FILES ); readfile ($ p ['tmp _ name']); // output the uploaded file TEXT; $ varname = 'My '; $ name = '3.txt '; $ type = 'text/plain '; $ key = "$ varname \"; filename = \ "$ name \ r \ nContent-Type: $ type \ r \ n "; $ fields [$ key] = $ contents; $ 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; &lt;/pre&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; http file Upload protocol, the boundary is used to identify the content and type of a file and the token of various Upload parameters. there is no difference between other and normal POST submission. &lt;Br/&gt; fsockopen is straightforward to write http requests. use curl to simulate the corresponding request header and body. &lt;Br/&gt; &lt;pre class = "sycode" name = "code"&gt; &lt;? Php // what file you want to upload $ uploadFile = file_get_contents ("/var/www/index.html"); // content boundary $ boundary = md5 (time ()); $ postStr = ""; $ postStr. = "--". $ boundary. "\ r \ n"; $ postStr. = "Content-Disposition: form-data; name = \" uptxt \ "; filename = \" index.html \ "\ r \ n"; $ postStr. = "Content-Type: text/html \ r \ n"; $ postStr. = $ uploadFile. "\ r \ n"; $ postStr. = "--". $ boundary. "\ r \ n";/** use fsockopen to set Upload http header and body **/$ fp = fsockopen ("localhost", "80", $ errer, $ errno, 1); fwrite ($ fp, "POST/upload. php HTTP/1.0 \ r \ n "); fwrite ($ fp," Content-Type: multipart/form-data; boundary = ". $ boundary. "\ r \ n"); fwrite ($ fp, "Content-length :". strlen ($ postStr ). "\ r \ n"); fwrite ($ fp, $ postStr); while (! Feof ($ fp) {echo fgets ($ fp, 128);} fclose ($ fp);/** use curl instead **/$ cl = curl_init (' http://localhost/upload.php '); $ Boundary = md5 (time (); curl_setopt ($ cl, CURLOPT_POST, true); curl_setopt ($ cl, CURLOPT_HTTPHEADER, array ("Content-Type: multipart/form-data; boundary = ". $ boundary); curl_setopt ($ cl, CURLOPT_POSTFIELDS, $ postStr); curl_setopt ($ cl, CURLOPT_RETURNTRANSFER, true); $ content = curl_exec ($ cl ); curl_close ($ cl); echo $ content;?&gt; &lt;/Pre&gt; &lt;br/&gt; upload. php &lt;br/&gt; &lt;pre class = "sycode" name = "code"&gt; &lt;? Php print_r ($ _ FILES);?&gt; &lt;/Pre&gt; &lt;br/&gt; Result &lt;br/&gt; &lt;pre class = "sycode" name = "code"&gt; HTTP/1.1 200 OKServer: nginx/0.8.54Date: tue, 10 Apr 2012 05:22:01 GMTContent-Type: text/htmlConnection: closeX-Powered-: PHP/5.3.10Array ([uptxt] =&gt; Array ([name] =&gt; index.html [type] =&gt; text/html [tmp_name] =&gt;/tmp/phpKHfxkY [error] =&gt; 0 [size] =&gt; 344 )) array ([uptxt] =&gt; Array ([name] =&gt; index.html [type] =&gt; text/html [tmp_name] = &gt;/Tmp/phpB0se13 [error] =&gt; 0 [size] =&gt; 344 )) &lt;/pre&gt; &lt;br/&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; complete PHP code &lt;br/&gt; $ contents = &lt;'text' &lt;br/&gt; The Content-Disposition in the datagram is &lt;br/&gt;: form-data; name = "userfile"; filename = "file_name" &lt;br/&gt; Content-Type: document type &lt;br/&gt; File content &lt;br/&gt; In this format &lt;br/&gt; The following is the server code &lt;br/&gt; curl_upload_server.php &lt;br/&gt; &lt;xmp&gt; &lt;br/&gt; &lt;? Php &lt;br/&gt; print_r (...... &lt;Br/&gt; no client? &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; complete code: PHP code &lt;br/&gt; $ contents = &lt;'text' &lt;br/&gt; The data in the datagram should be &lt;br /&gt; Content-Disposition: form-data; name = "userfile"; filename = "file_name" &lt;br/&gt; Content-Type: document type &lt;br/&gt; File content &lt;br/&gt; In this format &lt;br/&gt; The following is the server code &lt;br/&gt; curl_upload_server.php &lt;br/&gt; &lt;xmp&gt; &lt;br/&gt; &lt;? Php &lt;br/&gt; print_r (...... &lt;Br/&gt; It is best to develop it separately. thank you &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; http file Upload protocol, mainly boundary, this is to identify the content and type of a file and the token of various Upload parameters. there is no difference between other and common POST submissions. &lt;Br/&gt; fsockopen is straightforward to write http requests. use curl to simulate the corresponding request header and body. &lt;Br/&gt; PHP code &lt;br/&gt; &lt;? Php &lt;br/&gt; // what file you want to upload &lt;br/&gt; $ uploadFile = file_get _...... &lt;Br/&gt; Thank you very much. the CURL code has been successfully tested, I add another one hundred points &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; #14 servers &lt;br/&gt; #15 clients &lt;br/&gt; # $ contents in 14 is the content of the file to be uploaded &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; The last time you saw it, you asked about splitting multiple parts of a file, then upload. if so, all you need to do is use boundary to identify multiple Upload content blocks. For example, &lt;br/&gt; &lt;pre class = "sycode" name = "code"&gt; $ boundary = md5 (time (); $ postStr = ""; $ postStr. = "--". $ boundary. "\ r \ n"; $ postStr. = "Content-Disposition: form-data; name = \" uptxt [] \ "; filename = \" index_1.html \ "\ r \ n"; $ postStr. = "Content-Type: text/html \ r \ n"; $ postStr. = $ uploadFile. "\ r \ n"; # The content of some files $ postStr. = "--". $ boundary. "\ r \ n"; $ postStr. = "--". $ boundary. "\ r \ n"; $ postStr. = "Content-Disposition: form-data; Name = \ "uptxt [] \"; filename = \ "index_2.html \" \ r \ n "; $ postStr. = "Content-Type: text/html \ r \ n"; $ postStr. = $ uploadFile. "\ r \ n"; # The content of some files $ postStr. = "--". $ boundary. "\ r \ n"; &lt;/pre&gt; &lt;br/&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; Added to favorites! &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; to #21 &lt;br/&gt; you cannot do this, curl_multi concurrency should be used &lt;br/&gt; otherwise, several files will be split into one file, which will be output at a time and assembled on the server. What is the significance? &lt;Br/&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; The last time you saw a file split multiple copies and then uploaded, if so, all you need to do is to use boundary to identify multiple Upload content blocks. For example, &lt;br/&gt; PHP code &lt;br/&gt; $ boundary = md5 (time (); &lt;br/&gt; $ postStr = ""; &lt;br/&gt; $ postStr. = "--". $ boundary. "\ r \ n"; &lt;br/&gt; $ postStr. = "Content-Disposition: form-data; name = ...... &lt;Br/&gt; I used a loop to do this &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; reference the reply from the 21st floor: &lt;br/&gt; The last time you saw that you asked how many parts of a file were split and then uploaded. if so, all you need to do is to use boundary to identify multiple Upload content blocks. For example, &lt;br/&gt; PHP code &lt;br/&gt; $ boundary = md5 (time (); &lt;br/&gt; $ postStr = ""; &lt;br/&gt; $ postStr. = "--". $ boundary. "\ r \ n"; &lt;br/&gt; $ postStr. = "Content-Dispositi ...... &lt;Br/&gt; Thanks, curl_multi is very useful &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; to #21 &lt;br/&gt; you cannot do this, curl_multi concurrency should be used &lt;br/&gt; otherwise, several files will be split into one file, which will be output at a time and assembled on the server. What is the significance? &lt;Br/&gt; This is also true, but the concurrency of curl_multi makes sense for retrieving data after different URLs, that is, it is to request different URLs in parallel to obtain http responses. If you send a request to the same url, either one request or multiple requests are distributed. multiple requests consume scoket connection/destruction each time, but the number of request bytes can be controlled. &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; reference the reply from the 23rd floor: &lt;br/&gt; to #21 &lt;br/&gt; you cannot do this, curl_multi concurrency should be used &lt;br/&gt; otherwise, several files will be split into one file, which will be output at a time and assembled on the server. What is the significance? &lt;Br/&gt; This is also true, but the concurrency of curl_multi makes sense for retrieving data after different URLs, that is, it is to request different URLs in parallel to obtain http responses. If you send a request to the same url, either one request or multiple requests are distributed ...... &lt;Br/&gt; If the server supports keep-alive, you do not need to perform socket create multiple times, haha &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; One-way Upload cannot fully utilize network resources, multi-Channel concurrency can speed up the upload &lt;br/&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; reference the reply from the 26th floor: &lt;br/&gt; reference the reply from the 23rd floor: &lt;br/&gt; to #21 &lt;br/&gt; you cannot do this, curl_multi concurrency should be used &lt;br/&gt; otherwise, several files will be split into one file, which will be output at a time and assembled on the server. What is the significance? &lt;Br/&gt; This is also true, but the concurrency of curl_multi makes sense for retrieving data after different URLs, that is, it is to request different URLs in parallel to obtain http responses. If a request is sent to the same url, either the request is sent once or more ...... &lt;Br/&gt; well, keep-alive is basically supported. &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; One-way uploads cannot fully utilize network resources, multi-Channel concurrency can accelerate Upload &lt;br/&gt; well, higher network utilization &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; The CSDN editor has not been improved, manually reply to multiple responses at the same time... &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; well, the server can enable multiple threads/processes to process your concurrent Upload requests. &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; as long as the resources are sufficient, it is not a problem to open a few more steps. most of them are blocked, this is equivalent to the one-way &lt;br/&gt; design for multi-channel transmission. when a data transmission fails, you can re-transmit the data. &lt;Br/&gt; This is not a single-channel transmission design. &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; http://cn.php.net/fsockopen &lt;Br/&gt; CTRL + F search for boundary. let's take a look at the example and build an http request header for file upload. it is reasonable to say that CURL should build the same request header. &lt;Br/&gt; failing to follow the instructions in the PHP manual &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; reference the reply on the fifth floor: &lt;br/&gt; http://cn.php.net/fsockopen &lt;Br/&gt; CTRL + F search for boundary. let's take a look at the example and build an http request header for file upload. it is reasonable to say that CURL should build the same request header. &lt;Br/&gt; I failed to follow the instructions in the PHP Manual. &lt;br/&gt; right? It is better to use wireshark, smartsniff, and other tools to view the http request format (firebug or chrome F12 should also be supported), execute a file upload action, and then observe the http request format during File upload. &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; paste the post socket code: &lt;br/&gt; Backup I tested! &lt;Br/&gt; http://webinno.cn/blog/article/view/40 &lt;Br/&gt; &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; CURLOPT_HEADERFUNCTION can be used to set HTTP callback, you can refer to &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; curl as long as you set some parameters, then he will generate the protocol header and submit it to the server &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; Thank you! &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; I only used Content-Type for download. &lt;/p&gt; &lt;p class = "sougouAnswer"&gt; GREAT !!!!!! &lt;/P&gt; &lt;p class = "sougouAnswer"&gt; I also need one.
 

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.