Home >Backend Development >PHP Tutorial >PHP7 upload image function based on curl

PHP7 upload image function based on curl

不言
不言Original
2018-06-01 11:53:341832browse

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

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(&#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);

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:

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

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!

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