Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... P> PHP only needs to send a POST request to upload a file using CURL. in the request, set a field to the full path of the file to be uploaded and start, then, use CURL to send the variable to the server in the form of POST. on the server side, you can obtain the corresponding Upload file information from the Super global variable $ _ FILES.
The following is an example of this process.
Assume that there is a log.txt file in the path "/www/test/log.txt". The content is as follows:
this is a file for testhello PythonTab!
In order to upload this file to the server's script http://www.pythontab.com/upload.php, we wrote a script named curl_file.php in The Notebook, which contains the following content:
"@/Www/test/log.txt"); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); $ output = curl_exec ($ ch); curl_close ($ ch ); echo $ output;?>
The logic of curl_file.php is very simple. set the POST variable $ post_data, where upload points to the file to be sent. Note that we used POST to send a string, and then use file_get_contents ("php // input") on the server to obtain the string, which is different from the usage here, in fact, POST can also send key-value pairs like GET. On the server side, a super global variable $ _ POST can GET the value of the corresponding POST data like $ _ GET. Note that the variables to be uploaded do not exist in $ _ POST, but in $ _ FILES.
To demonstrate the logic of the file upload request received by the server, we wrote the following code in upload. php:
In the hosts file. The standard output of the script is as follows:
array( 'upload' => array( 'name' => 'log.txt', 'type' => 'application/octet-stream', 'tmp_name' => '/tmp/phpLUB59F', 'error' => 0, 'size' => 36, ) ) this is a file for test hello PythonTab!
The $ _ FILES variable contains an upload array, which corresponds to the description of the uploaded file. the name and type indicate the names and types respectively. . Generally, after receiving an uploaded file, the server needs to read the file immediately or copy the file to another file, because the temporary file referred to by tmp_name will be deleted after the server script is executed, upload. the last line of the php script is to copy the temporary file to our target file.