Curl command to simulate form upload file

Source: Internet
Author: User
  1. Initialization

  2. $ch = Curl_init ();

  3. To upload the local file address "@f:/xampp/php/php.ini" upload time, the upload path must be preceded by the @ symbol

  4. $furl = "@f:/xampp/php/php.ini";
  5. $post _data = Array (
  6. "Upload" = $furl
  7. );

  8. Where does curlopt_url mean to submit? Corresponds to the path specified by "Action" in the form

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

  10. Setting variables

  11. curl_setopt ($ch, Curlopt_url, $url);
  12. curl_setopt ($ch, Curlopt_returntransfer, 0);//Whether the execution result is returned, 0 is returned, and 1 is not returned
  13. curl_setopt ($ch, Curlopt_header, 0);//parameter settings, whether the header information is displayed, 1 is displayed, 0 is not displayed

  14. Forge a webpage source address and forge a form submission from Baidu

  15. curl_setopt ($ch, Curlopt_referer, "http://www.baidu.com");

  16. form data, is the normal form setting value is not 0

  17. curl_setopt ($ch, Curlopt_post, 1);

  18. curl_setopt ($ch, curlopt_timeout, 1);//Set the maximum amount of curl execution time-out

  19. When using arrays to provide post data, the Curl component is presumably intended to be compatible with the @filename of this upload file.

  20. By default the Content_Type is set to Multipart/form-data. Although for most Web servers and
  21. No impact, but there are still a few servers that are incompatible. This article concludes that there is no need to upload files in the
  22. case, try to http_build_query the data submitted by the post, and then send it out, to achieve better compatibility, smaller request packets.
  23. curl_setopt ($ch, Curlopt_postfields, $post _data);

  24. Execute and get results

  25. $output = curl_exec ($ch);
  26. if ($outopt = = = FALSE)
  27. {
  28. echo "
    "," CUrl Error: ". Curl_error ($ch);
  29. }
  30. Releasing the curl handle
  31. Curl_close ($ch);
  32. ?>

Copy Code

File 2:curl_post.php

    1. echo "
      ";
    2. Var_dump ($_files);
    3. ?>
Copy Code

The results are as follows:

    1. Array (1) {
    2. ["Upload"]=>
    3. Array (5) {
    4. ["Name"]=>
    5. String (7) "PHP.ini"
    6. ["Type"]=>
    7. String ("Application/octet-stream")
    8. ["Tmp_name"]=>
    9. String ("F:xampptmpphpb2d1.tmp")
    10. ["Error"]=>
    11. Int (0)
    12. ["Size"]=>
    13. Int (46217)
    14. }
    15. }
Copy Code
  • 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.