PHP7 PHP tips for uploading images based on curl

Source: Internet
Author: User
Tags php example
This article mainly introduced the PHP7 based on the curl implementation of the upload image function, combined with the case of a comparison between php5.5 and PHP7 version of the Curl image upload function related implementation and use of skills, the need for friends can refer to the next

This paper describes the upload image function of PHP7 based on curl. Share to everyone for your reference, as follows:

Depending on the PHP version, curl simulates the form upload method differently

Before php5.5

$curl = Curl_init (), if (Defined (' Curlopt_safe_upload ')) {  curl_setopt ($curl, Curlopt_safe_upload, false);} $data = Array (' file ' = ' @ '. Realpath ($path));//' @ ' symbol tells the server to upload resources curl_setopt ($curl, Curlopt_url, $url); curl_setopt ($ Curl, Curlopt_post, 1); curl_setopt ($curl, Curlopt_postfields, $data); curl_setopt ($curl, Curlopt_returntransfer, 1); curl_setopt ($curl, Curlopt_useragent, "TEST"), $result = Curl_exec ($curl); $error = Curl_error ($curl);

After php5.5, to PHP7

$curl = Curl_init (); curl_setopt ($curl, Curlopt_safe_upload, true); $data = Array (' file ' = ' = ' = new \curlfile (Realpath ($ path)); Url_setopt ($curl, Curlopt_url, $url); curl_setopt ($curl, Curlopt_post, 1); curl_setopt ($curl, Curlopt_ Postfields, $data); curl_setopt ($curl, Curlopt_returntransfer, 1); curl_setopt ($curl, Curlopt_useragent, "TEST"); $ result = Curl_exec ($curl); $error = Curl_error ($curl);

A compatible method is provided below:

$curl = Curl_init (), if (class_exists (' \curlfile ')) {curl_setopt ($curl, Curlopt_safe_upload, true); $data = Array (' file ' = = new \curlfile (Realpath ($path)))//>=5.5} else {if (defined (' Curlopt_safe_upload ')) {  curl_setopt ($curl, Curlopt_safe_upload, false); } $data = Array (' file ' = ' @ '. Realpath ($path));//<=5.5}curl_setopt ($curl, Curlopt_url, $url); curl_setopt ($curl, Curlopt_post, 1); curl_setopt ($curl, Curlopt_postfields, $data); curl_setopt ($curl, Curlopt_returntransfer, 1); Curl_ Setopt ($curl, Curlopt_useragent, "TEST"), $result = Curl_exec ($curl); $error = Curl_error ($curl);

which

$path: The address of the image to be uploaded

$url: Destination server address

For example

$url = "http://localhost/upload.php"; $path = "/bg_right.jpg"

upload.php Example:

<?php  file_put_contents (Time (). ". JSON ", Json_encode ($_files));  $tmp _name = $_files[' file ' [' Tmp_name '];  $name = $_files[' file ' [' name '];  Move_uploaded_file ($tmp _name, ' audit/'. $name);? >

Articles you may be interested in:

php5.0~5.6 version Compatibility Curl File Upload Function example analysis PHP tips

PHP Block Query Implementation method analysis PHP Tips

Compatible with PHP5 and php7 Curl File Upload function example analysis PHP tips

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.