The curl Command simulates form upload.

Source: Internet
Author: User
The curl Command simulates form upload.

  1. // Initialization

  2. $ Ch = curl_init ();

  3. // The address of the local file to be uploaded "@ F:/xampp/php. ini". the @ symbol must be added before the upload path.

  4. $ Furl = "@ F:/xampp/php. ini ";
  5. $ Post_data = array (
  6. "Upload" => $ furl
  7. );

  8. // CURLOPT_URL indicates where to submit it? It is equivalent to the path specified by "action" in the form.

  9. $ Url = "http: // localhost/test/curl/curl_post.php ";

  10. // Set variables

  11. Curl_setopt ($ ch, CURLOPT_URL, $ url );
  12. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0); // whether the execution result is returned; 0 indicates that the execution result is returned; 1 indicates that the execution result is not returned.
  13. Curl_setopt ($ ch, CURLOPT_HEADER, 0); // you can specify whether to display the header information. 1 indicates display, and 0 indicates not display.

  14. // Forge the source address of the webpage and submit a form from Baidu

  15. Curl_setopt ($ ch, CURLOPT_REFERER, "http://www.baidu.com ");

  16. // Form data, which is a regular form with a non-0 value

  17. Curl_setopt ($ ch, CURLOPT_POST, 1 );

  18. Curl_setopt ($ ch, CURLOPT_TIMEOUT, 1); // you can specify the maximum timeout value for curl execution.

  19. // When the array is used to provide post data, the CURL component probably aims to be compatible with the file uploading method like @ filename,

  20. // Content_type is set to multipart/form-data by default. For most web servers
  21. // No effect, but a small number of servers are not compatible. The conclusion in this article is that
  22. // In this case, perform http_build_query on the data submitted by post as much as possible, and then send it out to achieve better compatibility and smaller request data packets.
  23. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );

  24. // Execute and obtain the result

  25. $ Output = curl_exec ($ ch );
  26. If ($ outopt === FALSE)
  27. {
  28. Echo"
    "," CUrl Error: ". curl_error ($ ch );
  29. }
  30. // Release the cURL handle
  31. Curl_close ($ ch );
  32. ?>

File 2: curl_post.php

  1. Echo"
    ";
  2. var_dump($_FILES);
  3. ?>

The result is as follows:

  1. Array (1 ){
  2. ["Upload"] =>
  3. Array (5 ){
  4. ["Name"] =>
  5. String (7) "php. ini"
  6. ["Type"] =>
  7. String (24) "application/octet-stream"
  8. ["Tmp_name"] =>
  9. String (24) "F: xampptmpphpB2D1. tmp"
  10. ["Error"] =>
  11. Int (0)
  12. ["Size"] =>
  13. Int (46217)
  14. }
  15. }

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.