Example of PHP Uploading File through CURL

  • 2021-10-13 06:55:05
  • OfStack

In this paper, an example is given to describe the function of uploading files through CURL by PHP. Share it for your reference, as follows:

PHP only needs to send an POST request to upload files by using CURL. In the request, a certain field is set as the full path of the file to be uploaded, which begins with "@", and then the variable is sent to the server by POST using CURL, and the corresponding uploaded file information can be obtained from the super global variable $_ FILES on the server side.

Let's show this process with an example.

Suppose you have a local text file log. txt with the path "/www/test/log. txt" as follows:


this is a file for test
hello PythonTab!

In order to upload this file to the server script http://yourwebname.com/upload.php, we wrote a script named curl_file.php locally, which reads as follows:


<?php
$url = "http://yourwebname.com/upload.php";
$post_data = array(
"foo" => "bar",
// The address of the local file to upload 
"upload" = > "@/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 simple, setting the POST variable $post_data Where upload points to the file to be sent. Note here that when we used POST before, we all sent a string and then used it on the server side file_get_contents("php//input") To get the string, and the use here is not the same, in fact, POST can also be like GET1, send key-value pairs. On the server side, there is a super global variable $_ POST, which can get the corresponding POST data value like $_ GET1. Note that the variables for the uploaded file do not exist in $_POST, but in $_FILES.

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


<?php
echo var_export($_FILES,true);
echo file_get_contents($_FILES['upload']['tmp_name']);
copy($_FILES['upload']['tmp_name'], "./log_copy.txt");
?>

upload. php is first used var_export Will $_FILES Variable output to standard output, and then use the file_get_contents Read $_FILES['upload']['tmp_name'] The contents of the file referred to, and output it to standard output, and then put the $_FILES['upload']['tmp_name'] The file referred to is made into the log_copy. txt file in the current directory. The standard output of this 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!

You can see that in the $_ FILES variable there is an array of upload corresponding to the uploaded file description, where name and type represent the name and type, respectively. tmp_name is critical. After receiving the uploaded file, the server will write the file in a temporary file. The name of this temporary file is the value of tmp_name, which is why we can get the file contents of 1log. txt by reading this file. 1 After receiving the 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, and the last line of upload. php script is to copy the temporary file to our target file.

For more readers interested in PHP related content, please check the topics of this site: "php curl Usage Summary", "PHP Network Programming Skills Summary", "PHP Array (Array) Operation Skills Encyclopedia", "php String (string) Usage Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "PHP Operation and Operator Usage Summary"

I hope this paper is helpful to everyone's PHP programming.


Related articles: