由于我们的后台和前台是不同的php框架,而且后台和前台不在同一台服务器上,现在有一个问题,就是从后台上传图片要保存在前台的框架中。由于我们的后台和前台是不同的php框架,而且后台和前台不在同一台服务器上,现在有一个问题,就是从后台上传图片要保存在前台的框架中。 我们后台用的是laravel框架 public function laraveltest(Request $request) {$file = $request-file('file'); if($file - isValid
我们后台用的是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('上传成功');
}
}
这样就可以保存下来了!!!
沃梦达教程
本文标题为:PHP从一台服务器将图片发送到另一台服务器并保存
基础教程推荐
猜你喜欢
- php实现数组筛选奇数和偶数示例 2024-02-05
- PHP实现抽奖系统的示例代码 2023-06-26
- php数组函数序列之array_sum() – 计算数组元素值之和 2024-01-15
- 设定php简写功能的方法 2023-03-17
- Yii框架连表查询操作示例 2023-02-13
- PHP判断一个字符串是否是回文字符串的方法 2024-01-31
- PHP实现文件下载【实例分享】 2024-04-27
- PHP+MySQL+sphinx+scws实现全文检索功能详解 2023-01-31
- php实现构建排除当前元素的乘积数组方法 2022-11-23
- PHP手机短信验证码实现流程详解 2022-10-18
