由于我们的后台和前台是不同的php框架,而且后台和前台不在同一台服务器上,现在有一个问题,就是从后台上传图片要保存在前台的框架中。
我们后台用的是laravel框架
public function laraveltest(Request $request) {
$file = $request->file('file');
if($file -> isValid()){
$clientName = $file -> getClientOriginalName();
$tmpName = $file ->getFileName();
$realPath = $file -> getRealPath();
$entension = $file -> getClientOriginalExtension();
$newName = md5($clientName).".".$entension;
$url_path = 'upload/banner/';
$path = $file -> move($url_path,$newName);
$pathName = json_decode($path,TRUE);
$url1 = "http://47.101.54.26/test/testuploadpic";
$url2 = "http://47.101.54.27/test/testuploadpic";
$url3 = "http://47.101.54.28/test/testuploadpic";
$this->curl($path,$url1);
$this->curl($path,$url2);
$this->curl($path,$url3);
$returnPath = '/upload/banner/'.$newName;
return $this->json($returnPath);
}
}
public function curl($path,$url){
$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('file' => '@' . 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);
}
通过上面两个方法就可以将图片发送到另外一台服务器,我写了3台服务器是因为我想说明可以发送到无数台服务器
下面的代码是在另外一台服务器上接收到图片信息后保存图片
public function testuploadpic(){
$filename = $_FILES['file']['name'];
$tmpname = $_FILES['file']['tmp_name'];
$url = '/home/testuploadpic';
if(move_uploaded_file($tmpname, $url.$filename))
{
echo json_encode('上传成功');
}
}
这样就可以保存下来了!!!