How PHP uploads files via Curl

Source: Internet
Author: User
Tags php script

PHP using Curl to upload a file only need to send a POST request, set a field in the request to upload the full path of the file, and start with "@", and then use curl to send the variable post to the server, on the service side can be from the Super global variable $_ Files to the appropriate upload file information.

Let's show this process in an example.

Suppose there is a local text file Log.txt, whose path is "/www/test/log.txt", with the following contents:

This was a file for test
Hello pythontab!
In order to pass this file to the server script http://bbs.pythontab.com/upload.php, we wrote a script called curl_file.php, which reads:

1<?PHP2 $url= "http://bbs.pythontab.com/upload.php";3 $post _data=Array(4"foo" = "Bar",5 //local file address to upload6"Upload" = > "@/www/test/log.txt"7 );8 $ch=curl_init ();9curl_setopt ($ch, Curlopt_url,$url);Tencurl_setopt ($ch, Curlopt_returntransfer, 1); Onecurl_setopt ($ch, Curlopt_post, 1); Acurl_setopt ($ch, Curlopt_postfields,$post _data); - $output= Curl_exec ($ch); -Curl_close ($ch); the Echo $output; -?>


The logic of curl_file.php is simple, set the post variable $post_data, where upload points to the file that needs to be sent. Note here that we used post to send a string, and then use File_get_contents ("Php//input") on the server side to get the string, and the usage here is different, in fact post can also be like get, send key value pairs. There is a super global variable on the server $_post can get the value of the corresponding post data like $_get. It is important to note that the variables for uploading files are not in $_post, but in $_files.

To show the logic of the file upload request that the server received the above code, we wrote the following code in upload.php:

1 <? PHP 2 Echo Var_export ($_files,true); 3 Echo file_get_contents ($_files[' Upload '] [' tmp_name ']); 4 Copy ($_files[' Upload '] [' tmp_name '], "./log_copy.txt"); 5 ?>

upload.php first uses Var_export to output the $_files variable to standard output, and then uses file_get_contents to read the contents of the file referred to in $_files[' upload ' [' tmp_name '] and output to the standard output , and then make the $_files[' upload ' [' tmp_name '] files into the Log_copy.txt file of the current directory. 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 ' = +,file for Testhello Pythontab!


You can see that there is an upload array in the $_files variable that corresponds to the upload file description information, where name and type represent names and types, respectively. Tmp_name more critical, the service side after receiving the upload file, the file will be written in a temporary file, the name of the temporary file is the value of Tmp_name, which is why we read the file can get a Log.txt file content. Generally after the server receives the upload file will need to read the file immediately or copy the file into a file, because Tmp_name refers to the temporary file is deleted after the server script is executed, the last line of the upload.php script is to copy the temporary file into our target file.

How PHP uploads files via Curl

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.