-
-
//Initialization - $ch = curl_init();
//The local file address to be uploaded"@F:/xampp /php/php.ini" when uploading, there must be an @ symbol in front of the upload path
- $furl = "@F:/xampp/php/php.ini";
- $post_data = array (
- "upload" => $furl
- );
//Where does CURLOPT_URL refer to submission? Equivalent to the path specified by "action" in the form
- $url = "http://localhost/test/curl/curl_post.php";
//Set variables
- curl_setopt($ch , CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//Whether the execution result is returned, 0 is returned, 1 is not returned
- curl_setopt($ch, CURLOPT_HEADER, 0);//Parameter settings, Whether to display the header information, 1 means display, 0 means not display
//Fake web page source address, fake form submission from Baidu
- curl_setopt($ch, CURLOPT_REFERER, "http:// www.baidu.com");
//Form data, the regular form setting value is non-0
- curl_setopt($ch, CURLOPT_POST, 1);
- < ;p>curl_setopt($ch, CURLOPT_TIMEOUT, 1);//Set the maximum curl execution timeout
//When using an array to provide post data, the CURL component is probably for compatibility with @filename This way of writing uploaded files,
- //default content_type is set to multipart/form-data. Although it has no impact on most web servers, there are still a small number of servers that are incompatible. The conclusion drawn by this article is that when there is no need to upload files, try to http_build_query the data submitted by the post and then send it out, which can achieve better compatibility and smaller request data packets.
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//Execute and get the results
- $output = curl_exec($ch);
- if($outopt === FALSE)
- {
- echo "
","cUrl Error:".curl_error($ch);
- }
- // Release the cURL handle
- curl_close($ch);
- ?>
-
-
-
Copy code
File 2: curl_post.php
echo ""; - var_dump($_FILES);
- ?>
-
-
Copy the code
The results are as follows:
array(1) {- ["upload"]=>
- array(5) {
- ["name"]=>
- string(7) "php.ini"
- ["type" ]=>
- string(24) "application/octet-stream"
- ["tmp_name"]=>
- string(24) "F:xampptmpphpB2D1.tmp"
- ["error"]=>
- int(0 )
- ["size"]=>
- int(46217)
- }
- }
-
-
Copy code
|