1.从svn获取指定版本。
命令行:svn update svnworkpath --username xxx --password xxx -r head
$cmd = "svn update..."
$ar = array();
$status = 0;
exec($cmd, $ar, $status);
返回的结果包含以下内容:
A 002/ftp_upload.php U 002/main.php D 002/config.php
2.处理成本地路径和服务器上传路径:
function svnMessage($remoteArr,$localArr,$CONFIG_PG,$CONFIG_DIR) { /* * local dir */ for($ctr = 0;$ctr < count($localArr)-1; $ctr++) { //$localArr[$ctr] = str_replace ($CONFIG_FLG,'',$localArr[$ctr]); $istr = stripos($localArr[$ctr], $CONFIG_PG); $localArr[$ctr] = substr($localArr[$ctr],$istr); $localArr[$ctr] = str_replace ('\\','/',$localArr[$ctr]); } echo "本地路径:<br />\r\n"; for($ctr = 0;$ctr < count($localArr)-1; $ctr++) { echo $localArr[$ctr]; echo "<br />\r\n"; } /* * remote dir */ for($i = 0;$i < count($remoteArr)-1; $i++) { //$remoteArr[$i] = str_replace ($CONFIG_PG,'',$remoteArr[$i]); $imstr = stripos($remoteArr[$i], $CONFIG_PG); $remoteArr[$i] = substr($remoteArr[$i],$imstr+strlen($CONFIG_PG)); $remoteArr[$i] = $CONFIG_DIR.str_replace ('\\','/',$remoteArr[$i]); } echo "<br />\r\n上传路径:<br />\r\n"; for($i = 0;$i < count($remoteArr)-1; $i++) { echo $remoteArr[$i]; echo "<br />\r\n"; } }
3.连接到ftp
$conn = ftp_connect($ftp_ip) or die("Could not connect"); ftp_login($conn,$ftp_user,$ftp_pass);
4.上传时区分A,U,D和文件或文件夹
function ftp_handle_one($conn,$aud,$localfile,$remotefile) { echo "$aud,$localfile,$remotefile\r\n"; $r= true; if($aud == 'D'){ //删除时,找不到本地文件,无法判断是否文件夹。 try{ $r = ftp_delete($conn,$remotefile); echo "删除文件 $remotefile --> $r\r\n"; if(!$r){ $r = removeDir($conn,$remotefile); echo "删除目录 $remotefile --> $r\r\n"; } }catch(Exception $e){ echo '捕获异常: ' . $e->getmessage() . "<br/>错误代码:" . $e->getcode().'<br/>\r\n'; echo "------\r\n"; } }else if($aud == 'A'){ if(is_dir($localfile)){ $r = ftp_mkdir($conn,$remotefile); echo "创建目录 $remotefile --> $r\r\n"; }else{ $r = ftp_upload_one($conn,$localfile,$remotefile); echo "创建文件 $remotefile --> $r\r\n"; } }else if($aud == 'U'){ if(is_dir($localfile)){ //ftp_rmdir($conn,$remotefile); }else{ $r = ftp_upload_one($conn,$localfile,$remotefile); echo "更新文件 $remotefile --> $r\r\n"; } } }
5.递归删除一个目录
function removeDir($conn, $dirName ) { $arr = ftp_rawlist($conn,$dirName); //是个目录时,默认会有三行 //array(3) { // [0]=>string(7) "total 0" // [1]=>string(56) "drw-rw-rw- 1 user group 0 Sep 23 13:56 ." // [2]=>string(57) "drw-rw-rw- 1 user group 0 Sep 23 13:56 .." //} if(count($arr)>=3) { for($i = 3;$i < count($arr); $i++) { $isdir = substr($arr[$i],0,1); $filename = substr($arr[$i],55);//取文件名。 $filename = "$dirName/$filename"; echo "$isdir||$filename"; echo "\r\n"; if($isdir =="d") { removeDir($conn,$filename); }else{ ftp_delete($conn,$filename); } } ftp_rmdir($conn,$dirName); return true; }else{ return false; } }
6.上传一个文件
function ftp_upload_one($conn,$localfile,$remotefile) { // 开始 $fp = fopen($localfile, 'r'); $ret = ftp_nb_fput($conn, $remotefile, $fp, FTP_BINARY); while ($ret == FTP_MOREDATA) { // 加入其它要执行的代码 echo "...."; // 继续传送... $ret = ftp_nb_continue ($conn); } if ($ret != FTP_FINISHED) { echo "上传文件中发生错误..."; exit(1); } return true; }
感觉php对ftp,命令行支持得很好。脚本写得很方便。