直接上代码:
$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 为目标服务器地址