PHP7 upload image function based on curl

不言
Release: 2023-03-29 06:54:02
Original
1710 people have browsed it

This article mainly introduces the image upload function of PHP7 based on curl, and compares and analyzes the implementation and usage skills of the curl image upload function before php5.5 and php7 in the form of examples. Friends in need 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 curl simulation form upload method 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);
Copy after login

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);
Copy after login

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);
Copy after login

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"
Copy after login

upload.php example:

Copy after login

Related recommendations:

PHP implements multiple image upload and single image upload functions

PHP database operation class based on pdo [can support mysql, sqlserver and oracle]

PHP implements the function of preventing repeated submission of forms (based on token verification)

The above is the detailed content of PHP7 upload image function based on curl. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!