The curl Command simulates form upload.
-
// Initialization
- $ Ch = curl_init ();
-
// The address of the local file to be uploaded "@ F:/xampp/php. ini". the @ symbol must be added before the upload path.
- $ Furl = "@ F:/xampp/php. ini ";
- $ Post_data = array (
- "Upload" => $ furl
- );
-
// CURLOPT_URL indicates where to submit it? It is 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 indicates that the execution result is returned; 1 indicates that the execution result is not returned.
- Curl_setopt ($ ch, CURLOPT_HEADER, 0); // you can specify whether to display the header information. 1 indicates display, and 0 indicates not display.
-
// Forge the source address of the webpage and submit a form from Baidu
- Curl_setopt ($ ch, CURLOPT_REFERER, "http://www.baidu.com ");
-
// Form data, which is a regular form with a non-0 value
- Curl_setopt ($ ch, CURLOPT_POST, 1 );
-
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 1); // you can specify the maximum timeout value for curl execution.
-
// When the array is used to provide post data, the CURL component probably aims to be compatible with the file uploading method like @ filename,
- // Content_type is set to multipart/form-data by default. For most web servers
- // No effect, but a small number of servers are not compatible. The conclusion in this article is that
- // 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.
- Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
-
// Execute and obtain the result
- $ Output = curl_exec ($ ch );
- If ($ outopt === FALSE)
- {
- Echo"
"," CUrl Error: ". curl_error ($ ch );
- }
- // Release the cURL handle
- Curl_close ($ ch );
- ?>
File 2: curl_post.php
- Echo"
"; - var_dump($_FILES);
- ?>
The result is 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)
- }
- }
|