1. tp5 上传图片
$info = $file->move('文件路径');
2. 上传成功之后调用方法迁移图片(迁移方法在目的地服务器):
$url = 'http://destination.com/psms/upload/uploadImg?path=要保存图片的文件夹目录';
$data = array( 'file' => new \CURLFile(realpath('图片路径 + 名称')) );
$ch = curl_init();
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT,"TEST");
$result = curl_exec($ch);
// $error = curl_error($ch);
curl_close($ch); // 关闭CURL会话
if($result){
$return = 1;
}
3. 接收图片方法:
/**
* Notes : 添加单张图片
* User : SuLi
* DateTime : 2018/6/8 16:48
* @return int
*/
public function uploadImg(){
$path = $this->request->param('path'); // 接收 GET 方式传递过来的数据
// file_put_contents('json/'.time().".json", json_encode($_FILES)); // 把指定的内容写入指定的文件
$tmp_name = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
is_dir($path) OR mkdir($path, 0777, true);
$return = move_uploaded_file($tmp_name, $path.'/'.$name);
// $return = copy($tmp_name, 'audit/'.$name);
if($return){
return 1;
}else{
return 0;
}
}
/**
* Notes : 添加多张图片(未用到)
* User : SuLi
* DateTime : 2018/6/8 16:48
* @return int
*/
public function uploadAllImg(){
$time = time();
$path = $this->request->param('path'); // 接收 GET 方式传递过来的数据
// file_put_contents($time.".json", $path);
// file_put_contents('json/'.$time.".json", json_encode($_FILES)); // 把指定的内容写入指定的文件
$tmp_name = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
// $dir = './'.$path;
// is_dir($dir) OR mkdir($dir, 0777, true);
is_dir($path) OR mkdir($path, 0777, true);
$i = 0;
foreach ($name as $key=>$val) {
$return = move_uploaded_file($tmp_name[$i], $path.'/'.$name[$i]);
if($return){
$i++;
}else{
break;
}
unset($return);
}
// $return = copy($tmp_name, 'audit/'.$name);
if($i == count($name)){
// file_put_contents('json/'.$time."_success.json", 'OK!');
return 1;
}else{
// 删除已经保存的图片
// file_put_contents('json/'.$time."_error.json", 'NO!');
return 0;
}
}