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

PHP7 PHP skills for uploading images based on curl

jacklove
Release: 2023-04-01 19:08:02
Original
2966 people have browsed it

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);
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(&#39;file&#39; => &#39;@&#39; . 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:

<?php
  file_put_contents(time().".json", json_encode($_FILES));
  $tmp_name = $_FILES[&#39;file&#39;][&#39;tmp_name&#39;];
  $name = $_FILES[&#39;file&#39;][&#39;name&#39;];
  move_uploaded_file($tmp_name,&#39;audit/&#39;.$name);
?>
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template