时间:12-08-24 栏目:网站开发 作者:admin 评论:0 点击: 9,103 次
之前写过一个通过FTP上传SVN版本到指定服务器的,可参考:PHP提取SVN信息并上传到FTP,
有些服务器没有开通FTP但有SSH,可以用下面的方法实现,支持FTP和SFTP两种方式。
PHP提取SVN信息并上传到SFTP(通过SSH),代码如下:
<?php /** * * PHP提取SVN信息并上传到FTP * $RCSfile: svn.php $ * $Author: Steven.liao 青剑 * $Revision: 2.0.0 $ * $Date: 2012/08/24 21:50:00 $ */ //参数配置 $ftptype = 'sftp';//ftp,sftp $ftpserver = '';//ftp server $ftpuser = 'root';//ftp user name $ftppassword = '';//ftp user password $ftpport = 22;//端口 $ftpsubdir = '/var/www/html/'; //FTP下目录 $svnrepository = "D:\\wwwroot\\\\";//版本库副本所在的路径 $svnbinpath = "D:\\Program Files\\CollabNet\\Subversion Client\\svn.exe";//svn可执行文件路径 set_include_path('api/ssh/' . PATH_SEPARATOR . 'phpseclib'); @include_once("api/ssh/Net/SSH2.php"); @include_once("api/ssh/Net/SFTP.php"); function flush2 (){ echo(str_repeat(' ',256)); // check that buffer is actually set before flushing if (ob_get_length()){ @ob_flush(); @flush(); @ob_end_flush(); } @ob_start(); } /*得到精确时间*/ function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } function get_use_time(){ global $time_start; $time_end = getmicrotime(); $times = $time_end - $time_start; $use_time = "<br />用时:". $times ."秒<br />"; return $use_time; } function replace_path($str){ $str = str_replace('\\/','\\',$str); $str = str_replace('/','\\',$str); return $str; } date_default_timezone_set('PRC'); set_time_limit(0); header("Content-type:text/html; charset=utf-8"); flush(); $time_start = getmicrotime(); //计算时间开始 set_time_limit(0); echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>按SVN版本信息上传</title> <style> #left, #right{ width:46%; float:left; background:#CCCCCC; border:#999999 1px solid; margin:1%; } </style> </head> <body>'; flush(); if($v1 = $_GET['s']) {//版本号 $r = array();//执行外面svn命令返回的信息数组 $f = array();//匹配文件 $fa = array();//所有需上传的匹配文件 if($v2 = $_GET['e']) {//至版本号(从起始版本号至版本号所有文件) exec('"'.$svnbinpath."\" log $svnrepository -r {$v1}:{$v2} -q -v",$r,$return); } else {//单版本号 exec('"'.$svnbinpath."\" log $svnrepository -r {$v1} -q -vd",$r,$return); } foreach($r as $value) { if(preg_match("/^[ ]*[AM]{1} (\/.+)$/i",$value,$f)) {//符合文件更新规则的文件名放入上传数组 $fa[] = $f[1]; } } echo "<div id=left><h3>revision: $v1 ".(($v2)?" - $v2":'')." 需要上传的文件:\n</h3><ol>"; foreach($fa as $value) { echo "<li>".replace_path($value) ."</li>\n"; flush(); } echo "</ol></div>\n"; flush(); if($fa) { if($ftptype=='sftp'){ echo "<div id=right> <h3>"; $ssh = new Net_SSH2($ftpserver,$ftpport); $sftp = new Net_SFTP($ftpserver,$ftpport); if (!@$ssh->login($ftpuser,$ftppassword)) { // $err[]='与节点服务器通讯失败,无法连接节点服务器SSH。ERROR_CODE:SSHCONNECT'; $error = true; echo '<p>与节点服务器SSH通讯失败</p>'; flush2(); } else { if (!@$sftp->login($ftpuser,$ftppassword)) { // $err[]='与节点服务器通讯失败,无法连接节点服务器SFTP。ERROR_CODE:SFTPCONNECT'; $error = true; echo '<p>与节点服务器SFTP通讯失败</p>'; flush2(); } else { echo '<p>与节点服务器SSH&SFTP通讯成功</p>'; flush2(); } } if ( @$ssh && @$sftp ) { // 连接SSH和SFTP是否成功 if (!$sftp->chdir($ftpsubdir)){ // $err[]='与节点服务器通讯失败,无法切换到虚拟主机配置文件目录。ERROR_CODE:CHDIRVHOST'; echo '<p>无法切换到虚拟主机配置文件目录</p>'; }else{ echo "</h3><ol>"; //echo $sftp->pwd() . "\r\n"; //$sftp->put('cache/filename.ext', 'hello, world!'); //print_r($sftp->nlist()); //$s = $ssh->exec("ls -sh $ftpsubdir"); //创建虚拟主机配置文件存放目录 //echo nl2br($s); foreach($fa as $value) { $title = ''; $remote_path = './'.str_replace('\\','/',$value); $local_path = replace_path($svnrepository.$value); //$upload = ftp_put($conn_id,$remote_path, $local_path, FTP_BINARY); // echo "</ol></div><div style='clear:both'></div><div id=foot>上传完成[<a href=\"?\">返回</a>]"; if(is_dir($local_path)){ $s = $ssh->exec("mkdir $ftpsubdir/$remote_path"); $title = nl2br($s); }else{ $upload = $sftp->put($remote_path , file_get_contents($local_path)); } if($upload) { echo "<li title=\"$title\">upload $remote_path <font color=\"green\">:)</font> </li>\n"; } else { echo "<li title=\"$title\">upload $remote_path <font color=\"red\">:(</font> </li>\n"; } flush(); } echo "</ol></div><div style='clear:both'></div><div id=foot>上传完成[<a href=\"?\">返回</a>]"; } } }else{ $conn_id = ftp_connect($ftpserver,$ftpport); $login_result = ftp_login($conn_id,$ftpuser,$ftppassword); echo "<div id=right> <h3>"; if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed! :(</h3></div>\n"; exit(); } else { echo "Connected to $ftpserver ftp\n"; } // echo "<LI>Current directory: " . ftp_pwd($conn_id) . "</LI>\n"; if($ftpsubdir){ if (ftp_chdir($conn_id, $ftpsubdir)) { echo " (" . ftp_pwd($conn_id) . ")\n"; } else { echo " Couldn't change directory\n"; exit(); } } echo "</h3><ol>"; foreach($fa as $value) { $remote_path = './'.str_replace('\\','/',$value); $local_path = replace_path($svnrepository.$value); $upload = ftp_put($conn_id,$remote_path, $local_path, FTP_BINARY); // echo "</ol></div><div style='clear:both'></div><div id=foot>上传完成[<a href=\"?\">返回</a>]"; if($upload) { echo "<li>upload $remote_path <font color=\"green\">:)</font> </li>\n"; } else { echo "<li>upload $remote_path <font color=\"red\">:(</font> </li>\n"; } flush(); } echo "</ol></div><div style='clear:both'></div><div id=foot>上传完成[<a href=\"?\">返回</a>]"; ftp_close($conn_id); } } flush(); } else {//无参数提示 ?> 请输入版本号参数s,如需连续多个版本则加上参数e <form action="" method="get"> <p>版本库 从: <input name="s" value="" size="5" /> 到: <input name="e" value="" size="5" /> (可以不填) <input name="sub" value="开始上传" type="submit" /> </p> </form> <? exec('"'.$svnbinpath."\" log $svnrepository -q -r head -l 10",$r,$return); echo iconv('GBK','UTF-8',implode("<br>\r\n",$r)); exec('"'.$svnbinpath."\" log $svnrepository -q -l 10",$r,$return); echo iconv('GBK','UTF-8',implode("<br>\r\n",$r)); } ?>
将上面的代码复制保存并命名为svn_upload.php,请自行修改好配置参数,
然后放在本地路径,比如:http://localhost/svn_upload.php,访问此URL即可。
SSH2 API可以在:http://phpseclib.sourceforge.net/ 下载
声明: 本文由( admin )原创编译,转载请保留链接: PHP提取SVN信息并上传到SFTP(通过SSH)
------====== 本站公告 ======------
联系信息:
电话:19970108113(微信同号) QQ:3142401606
支付宝:https://me.alipay.com/lxq73061
相关插件程序等信息均会在站内发布,敬请关注。