标签:
要想sftp服务端发送文件,就需要php脚本具有作为ssh客户端的能力,所以需先为php安装如下扩展
按照下面的命令进行安装
1 rpm -qa | grep openssl 2 wget http://www.libssh2.org/download/libssh2-1.6.0.tar.gz 3 tar zxvf llibssh2-1.6.0.tar.gz 4 cd libssh2-1.6.0 5 ./configure 6 make && make install 7 wget http://pecl.php.net/get/ssh2-0.11.3.tgz 8 tar xvf ssh2-0.11.3.tgz 9 cd ssh2-0.11.3 10 phpize 11 ./configure --with-ssh2 12 make 13 cp modules/ssh2.so /alidata/server/php-5.4.23/lib/php/extensions/no-debug-non-zts-20100525 14 echo ‘extension=ssh2.so‘ >> /alidata/server/php-5.4.23/etc/php.ini 15 service nginx restart 16 php -i | grep ssh2
服务器软件安装路径需要根据实际情况修改
注意这里服务端以及处理完文件上传,这个推送的部分可以另起一个线程或进程去推送
1 public function proccessFile($destinationPath, $filename) 2 { 3 //parent::proccessFile($destinationPath, $filename); // TODO: Change the autogenerated stub 4 5 $remoteBasePath = ‘/mnt/java/pipeline/ads/upload/‘; 6 $localBasePath = ‘/alidata/www/aft/‘; 7 $connection = ssh2_connect(‘XXX.XXX.XXX.XXX‘, 22); 8 ssh2_auth_password($connection, ‘username‘, ‘password‘); 9 /* 10 $sftp = ssh2_sftp($connection); 11 SiteHelpers::PE($sftp); 12 $resFile = fopen("ssh2.sftp://$sftp/mnt/java/pipeline/ads/upload", ‘w‘); 13 $srcFile = fopen(‘/alidata/www/aft/‘.$destinationPath.‘/‘.$filename,‘r‘); 14 $writtenBytes = stream_copy_to_stream($srcFile, $resFile); 15 fclose($resFile); 16 fclose($srcFile); 17 */ 18 $dateDir = date("Y-m/d",strtotime("-1 day")); 19 // SiteHelpers::PE($remoteBasePath.$dateDir.‘/‘.$filename); 20 // $sftp = ssh2_sftp($connection); 21 // ssh2_sftp_mkdir($sftp,$remoteBasePath.$dateDir); 22 ssh2_exec($connection,‘mkdir -p ‘.$remoteBasePath.$dateDir); 23 ssh2_scp_send($connection, 24 $localBasePath.$destinationPath.‘/‘.$filename, 25 $remoteBasePath.$dateDir.‘/‘.$filename); 26 27 Log::info(‘处理了投放数据‘); 28 Log::info($destinationPath.‘/‘.$filename); 29 }
可以看到最后文件成功推送到sftp服务器
标签:
原文地址:http://www.cnblogs.com/lvyahui/p/4695764.html