PHP基于curl实现的上传图片功能

来自:互联网
时间:2018-12-31
阅读:
免费资源网 - https://freexyz.cn/

直接上代码:

$filename = GUIDv4() . '.jpg';
$save_path = $save_dir . DIRECTORY_SEPARATOR . $filename;
file_put_contents($save_path, $data);


$curl = curl_init();if (class_exists('CURLFile')) {
 curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
$data = array('file' => new CURLFile(realpath($save_path)));//>=5.5} else { if (defined('CURLOPT_SAFE_UPLOAD')) {
  curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
 }
 $data = array('file' => '@' . realpath($save_path));//<=5.5}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$result = curl_exec($curl);

$save_path 为图片路径

$url 为目标服务器地址

免费资源网 - https://freexyz.cn/
返回顶部
顶部