Home>Article>Backend Development> PHP7 PHP skills for uploading images based on curl

PHP7 PHP skills for uploading images based on curl

jacklove
jacklove Original
2018-06-25 16:59:30 2833browse

This article mainly introduces the image upload function of PHP7 based on curl, and compares and analyzes the relevant implementation and usage skills of the curl image upload function before php5.5 and php7 in the form of examples. Friends who need it can refer to it

The example in this article describes the image upload function implemented by PHP7 based on curl. Share it with everyone for your reference, the details are as follows:

According to different php versions, the method of curl simulation form upload is different

Before php5.5

$curl = curl_init(); if (defined('CURLOPT_SAFE_UPLOAD')) { curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false); } $data = array('file' => '@' . realpath($path));//‘@' 符号告诉服务器为上传资源 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, go 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);

The following provides a compatible Method:

$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);

Among them:

$path: is the address of the image to be uploaded

$url: Target server address

For example

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

upload.php example:

Articles you may be interested in:

PHP5.0~5.6 Compatibility of each version cURL file upload function example analysis php Tips

PHP block query implementation method analysis php tips

Example analysis of cURL file upload function compatible with php5 and php7 php Skill

The above is the detailed content of PHP7 PHP skills for uploading images based on curl. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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