yimingwang

情景:客户端上传图片到服务器A,服务器A同步上传至另外一个静态资源服务器B

环境:php7 linux(ubuntu)

安装php的ssh2扩展

sudo apt-get install libssh2-1-dev

sudo  apt-get install php-ssh2

sudo service apache2 restart

可在图片上传至服务器A后同步上传至B

A上传文件至B  函数

 1 //scp上传文件至远程服务 $host为B服务器域名(IP) $user B服务器用户 $password B服务器密码 $local_file为本地文件, $remote_file为远程文件
 2 function scpUploadFile($host,$user,$password,$local_file,$remote_file){
 3     $ssh2 = ssh2_connect($host, 22);  //先登陆SSH并连接
 4     ssh2_auth_password($ssh2,$user,$password);//身份认证  也可以用
 5     //本地传输文件到远程服务器
 6     $stream=ssh2_scp_send($ssh2, $local_file, $remote_file, 0777);
 7     $data =[\'host\'=>$host,\'user\'=>$user,\'pass\'=>$password,\'lo\'=>$local_file,\'re\'=>$remote_file];
 8 
 9     //默认权限为0644,返回值为bool值,true或false.
10     return $stream;
11 }

A从B下载文件 函数

1 function scpDownloadFile($host,$user,$password,$local_file,$remote_file){
2     $ssh2 = ssh2_connect($host, 22);
3     ssh2_auth_password($ssh2,$user,$password);
4     //从远程服务器下载文件
5     $stream=ssh2_scp_revc($ssh2, $remote_file, $local_file);
6     return $stream;
7 }

上述连接及身份认证方式 可换为SSH密钥链接 

1 $ssh2 = ssh2_connect(\'shell.example.com\', 22, array(\'hostkey\'=>\'ssh-rsa\'));
2 
3 if (ssh2_auth_pubkey_file($connection, \'username\',
4                           \'/home/username/.ssh/id_rsa.pub\',
5                           \'/home/username/.ssh/id_rsa\', \'secret\')) {
6   echo "Public Key Authentication Successful\n";
7 } else {
8   die(\'Public Key Authentication Failed\');
9 }

 

简单处理客户端多图片上传请求(处理粗糙,可自行完善)

多图片上传数组处理

 1 function buildImgArray($_FILES){
 2     $i = 0;
 3     foreach ($files as $v){//三维数组转换成2维数组
 4         if(is_string($v[\'name\'])){ //单文件上传
 5             $info[$i] = $v;
 6             $i++;
 7         }else{ // 多文件上传
 8             foreach ($v[\'name\'] as $key=>$val){//2维数组转换成1维数组
 9                 //取出一维数组的值,然后形成另一个数组
10                 //新的数组的结构为:info=>i=>(\'name\',\'size\'.....)
11                 $info[$i][\'name\'] = $v[\'name\'][$key];
12                 $info[$i][\'size\'] = $v[\'size\'][$key];
13                 $info[$i][\'type\'] = $v[\'type\'][$key];
14                 $info[$i][\'tmp_name\'] = $v[\'tmp_name\'][$key];
15                 $info[$i][\'error\'] = $v[\'error\'][$key];
16                 $i++;
17             }
18         }
19     }
20     return $info;
21 }

上传至A并同步上传至B

 1 function uploadFile($files,$host,$user,$password,$maxSize=1048576,$imgFlag=true){
 2     $date = getdate(time());
 3     $year = $date[\'year\'];
 4     $mon = $date[\'mon\'];
 5     $day = $date[\'mday\'];
 6     $path = \'upload/\';
 7     if (! is_dir($path)) {
 8         mkdir($path,0777,true);
 9     }
10     $i = 0;
11     foreach ($files as $val) {
12         if ($val[\'error\'] == 0) {
13             if($val[\'size\']>$maxSize){
14                 echo "文件太大了";
15                 return 1;
16             }
17             if($imgFlag){
18                 $result = getimagesize($val[\'tmp_name\']);
19                 if(!$result){
20                     echo "您上传的不是一个真正图片";
21                     return 2;
22                 }
23             }
24             if(!is_uploaded_file($val[\'tmp_name\'])){
25                 echo "不是通过httppost传输的";
26                 return 3;
27             }
28             $realName = $year.$mon.$day.time().$val[\'name\'];
29             $destination = $path."/".$realName;
30             if(move_uploaded_file($val[\'tmp_name\'], $destination)){
31                 $isUp = scpUploadFile($host,$user,$password,$destination,\'/upload/\'.$realName);
32                 if(!$isUp){
33                     return 4;
34                 }
35                 $uploadedFiles[$i][\'img\']=\'/upload/\'.$realName;
36                 $i++;
37             }
38         }else {
39             echo \'上传失败\';
40             return 5;
41         }
42     }
43     return $uploadedFiles;
44 }

 

分类:

技术点:

相关文章:

  • 2021-09-01
  • 2021-11-24
  • 2021-11-13
  • 2021-11-29
  • 2022-12-23
  • 2021-07-21
  • 2021-11-13
  • 2022-12-23
猜你喜欢
  • 2021-11-13
  • 2021-11-13
  • 2021-08-09
  • 2022-01-08
  • 2021-09-16
相关资源
相似解决方案