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