PHP Curl Upload File code example, _php tutorial

Source: Internet
Author: User
Tags curl upload file

PHP Curl Upload file Code instance,


Suppose the server-side upload file processing script upload.php:
Copy the Code code as follows:
<?php

Print_r ($_post);
Print_r ($_files);

1, using the default method of CURL
Copy the Code code as follows:
If the PHP file is UTF8 encoded, the system is GBK encoded, then you need to turn the code, or PHP can not find this file in the system
$file = Realpath (mb_convert_encoding (' Test picture. JPG ', ' GBK ', ' UTF8 ');

$file = Realpath (' temp.jpg '); The file to upload
$fields [' f '] = ' @ '. $file; Preceded by an @ symbol to upload a picture

$ch =curl_init ();


curl_setopt ($ch, Curlopt_url, ' http://localhost/upload.php ');

curl_setopt ($ch, curlopt_post,true);
curl_setopt ($ch, Curlopt_postfields, $fields);
curl_setopt ($ch, curlopt_returntransfer,true);


$content = curl_exec ($ch);

Echo $content;

2, the alternative approach , sometimes we need to be dynamically generated content as a file upload to the remote server, but do not want to build a temporary file on the local server. This is the alternative way of writing.

Copy the Code code as follows:
$contents =<<< ' TEXT '
Here is the file content, can also be the picture binary, the picture needs to modify the upload file type
TEXT;

$varname = ' my ';//upload to key in the $_files array
$name = ' 3.txt ';//File name
$type = ' text/plain ';//File type

$key = "$varname \"; Filename=\ "$name \r\ncontent-type: $type \ r \ n";
$fields [$key] = $contents;



$ch =curl_init ();


curl_setopt ($ch, Curlopt_url, ' http://localhost/upload.php ');

curl_setopt ($ch, curlopt_post,true);
curl_setopt ($ch, Curlopt_postfields, $fields);
curl_setopt ($ch, curlopt_returntransfer,true);


$content = curl_exec ($ch);

Echo $content;

http://www.bkjia.com/PHPjc/990551.html www.bkjia.com true http://www.bkjia.com/PHPjc/990551.html techarticle php Curl Upload file Code instance, assuming the server side upload file processing script upload.php: Copy code code as follows: PHP print_r ($_post); Print_r ($_files); 1, using Curl Default party ...

  • Related Article

    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.