文章是转载的,不过已经验证可用。需要注意的是,图片的路径一定要是绝对地址,例如:“@D:/imgs/01/1.png”
test.php
<?php
include ‘common.inc.php’;
header(‘content-type:text/html;charset=utf8’);
$ch = curl_init();
//加@符号curl就会把它当成是文件上传处理
$data = array(‘img’=>’@’. dirname(__FILE__).’/img/2.png’);
curl_setopt($ch,CURLOPT_URL,”https://www.libaocai.com/get_img.php”);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
sys_debug($data);
$result = curl_exec($ch);
curl_close($ch);
echo json_decode($result);
?>
get_img.php
<?php
if($_FILES){
$filename = $_FILES[‘img’][‘name’];
$tmpname = $_FILES[‘img’][‘tmp_name’];
if(move_uploaded_file($tmpname,dirname(__FILE__).’/’.$filename)){
echo json_encode(‘上传成功’);
}else{
$data = json_encode($_FILES);
echo $data;
}
}
?>
转载请注明:苏demo的别样人生 » php 通过curl上传图片