Home > Backend Development > PHP Tutorial > php curl upload file

php curl upload file

WBOY
Release: 2016-07-25 08:42:11
Original
1076 people have browsed it
Assume that the server-side upload file processing script upload.php:
  1. print_r($_POST);
  2. print_r($_FILES);
Copy code

1. Use CURL default method

  1. //If the php file is utf8 encoded and the system is GBK encoded, then you need to change the encoding, otherwise Php cannot find this file in the system
  2. $ file = realpath(mb_convert_encoding('Test image.JPG','GBK','utf8'));
  3. $file = realpath('temp.jpg'); //File to be uploaded
  4. $fields['f' ] = '@'.$file; // Add the @ symbol in front to indicate uploading pictures
  5. $ch =curl_init();
  6. curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php'); ECurl_Setopt ($ ch, curlopt_post, true); Rue); c $ content = curl_exec ($ ch);
  7. echo $content;
  8. Copy code
2.
An alternative approach, sometimes we need to upload dynamically generated content to a remote server as a file, but we don’t want to build a temporary file in the local server. This way we have this alternative way of writing


$contents =<<< 'TEXT'This is the file content, or it can be a binary image. The image needs to be modified to upload the file type
TEXT;
$varname = 'my';//Upload To the key in the $_FILES array
$name = '3.txt';//File name
    $type = 'text/plain';//File type
  1. $key = "$varname"; filename="$namernContent -Type: $typern";
  2. $fields[$key] = $contents;
  3. $ch =curl_init();
  4. curl_setopt($ch,CURLOPT_URL,'http://localhost/upload.php' );
  5. curl_setopt($ch,CURLOPT_POST,true);
  6. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  7. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  8. $content = curl_exec($ch);
  9. echo $content;
  10. Copy code
Upload files, php, curl


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template