公司的代码发在上ftp上传的,无奈只能手动递归生成目录和文件,然后从生产服务器上ftp下来做好备份,再然后从测试服务器上拉对应的代码下来,最后上传到生产服务器上
写这个脚本只是为了做好备份恢复和上传代码方便

输入图片说明输入图片说明

代码检测

输入图片说明输入图片说明

上代码
<?php
	ob_start();
	set_time_limit(0);
	header("Content-type: text/html; charset=utf-8");
	// 项目文件夹
	$obj = 'test';
	// 生产备份
	$bak = 'bak';
	// 测试目录
	$test = 'test';
	// 版本号
	$version = '';
	$botton_select = 'make';
	$content = '';
	$success = '';
	$msg = '';
	$bak_file_num = $bak_file_faile_num = $test_file_num = $test_file_fail_num = 0;
	// 检测结果
	$bak_check = $test_check = true;
	if(!empty($_POST)){
		$version = $_POST['version'];
		if(empty($version)){
			$msg .= '请填写版本号...<br />';
		}
		$botton_select = $_POST['botton'];
		$content = $_POST['content'];
		if(empty($content)){
			$msg .= '请填写文件目录...<br />';
		}else{
			$file_arr = explode("\r\n",$content);
			$file_arr = array_unique($file_arr);
			$content = implode("\r\n",$file_arr);
			$file_arr_num = count($file_arr);
			$success .= "共有文件目录{$file_arr_num}个...<br />";	
			foreach ($file_arr as $key => $value) {
				// 生成目录
				if($botton_select == 'make'){
					##### 备份的
					$bak_file = $obj . '/' . $bak . '/' . $version . "/" . $value;
					$bak_file_str = strripos($bak_file,'/');
					// 递归中最后的文件夹
					$bak_file_dir = substr($bak_file,0,$bak_file_str);
					$make_bak_file_dir = makeDir($bak_file_dir);
					$make_bak_file_res = makeFile($bak_file);
					if(!$make_bak_file_res){
						$bak_file_faile_num += 1;
						$msg .= "生成{$bak_file}失败...<br />";
					}else{
						$bak_file_num += 1;
					}
					##### 测试里面要上线的
					$test_file = $obj . '/' . $test . '/' . $version . "/" . $value;
					$test_file_str = strripos($test_file,'/');
					// 递归中最后的文件夹
					$test_file_dir = substr($test_file,0,$test_file_str);
					$make_test_file_dir = makeDir($test_file_dir);
					$make_test_file_res = makeFile($test_file);
					if(!$make_test_file_res){
						$test_file_fail_num += 1;
						$msg .= "生成{$test_file}失败...<br />";
					}else{
						$test_file_num += 1;
					}
				}else{
					// 备份的
					$bak_file = $obj . '/' . $bak . '/' . $version . "/" . $value;
					if(!file_exists($bak_file)){
						$msg .= "备份文件{$bak_file}不存在...<br />";
						$bak_check = false;
					}else{
						$bak_file_cont = file_get_contents($bak_file);
						if(strlen($bak_file_cont) == 0){
							$msg .= "备份文件{$bak_file}内容为空...<br />";
							$bak_check  = false;
						}
					}
					// 测试里面要上线的
					$test_file = $obj . '/' . $test . '/' . $version . "/" . $value;
					if(!file_exists($test_file)){
						$msg .= "上线文件{$test_file}不存在...<br />";
						$test_check = false;
					}else{
						$test_file_cont = file_get_contents($test_file);
						if(strlen($test_file_cont) == 0){
							$msg .= "上线文件{$test_file}内容为空...<br />";
							$test_check = false;
						}
					}
				}
			}
			//  生成目录
			if($botton_select == 'make'){
				$success .= "生成备份文件成功{$bak_file_num}个...<br />";
				if($bak_file_faile_num){
					$msg .= "生成备份文件失败{$bak_file_faile_num}个...<br />";
				}
				$success .= "生成上线文件成功{$test_file_num}个...<br />";
				if($test_file_fail_num){
					$msg .= "生成上线文件失败{$test_file_fail_num}个...<br />";
				}
			}else{
				// 检查 
				if($bak_check == false){
					$msg .= "备份代码检测不通过...<br />";
				}else{
					$success .= "备份代码检测通过...<br />";
				}
				if($test_check == false){
					$msg .= "上线代码检测不通过...<br />";
				}else{
					$success .= "上线代码检测通过...<br />";
				}
			}
		}
	}

	/**
	 * [makeDir description]
	 * @param  string $file [description]
	 * @return [type]       [description]
	 */
	function makeDir($dir = '')
    {
        $str = '';
        if($dir && !is_dir($dir)){
            $str = "mkdir -p " . $dir;
        }else{
            return false;
        }
        $res = system($str,$retval);
        if($retval == 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * [makeFile description]
     * @param  string $file [description]
     * @return [type]       [description]
     */
    function makeFile($file = '')
    {
    	$str = '';
        if($file && !file_exists($dir)){
            $str = "touch " . $file;
        }else{
            return false;
        }
        $res = system($str,$retval);
        if($retval == 0){
            return true;
        }else{
            return false;
        }
    }

?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>test上线</title>
	<link rel="stylesheet" href="">
</head>
<body>
	<style type="text/css" media="screen">
		form{
			font-size: 16px;
			width: 400px;
			margin-left: 100px;
		}
		select{
			width: 200px;
			/*display: block;*/
			margin: 20px 0;
			font-size: 15px;
		}
		textarea{
			width: 500px;
			height: 400px;
			/*display: block;*/
		}
		button{
			width: 100px;
			font-size: 15px;
		}
		.msg{
			margin-left: 100px;
			color: red;
		}
		input{
			width: 200px;
			height: 20px;
		}
		p{
			display: inline;
		}
		.obj{
			margin:10px 0; 
			margin-left: 100px;
		}
		.success{
			margin-left: 100px;
		}
	</style>
	<div class="success">
		<?php echo $success; ?>
	</div>
	<div class="msg">	
		<?php echo $msg; ?>
	</div>
	<div class="obj">
		<label>项目:</label>
		<p><?php echo $obj; ?></p>
	</div>
	<form action="" method="post" accept-charset="utf-8">
		<div>
			<label for="version">请填写版本号:</label>
			<input type="text" id="version" name="version" placeholder="请填写版本号" value="<?php echo $version; ?>">
		</div>
		<div>		
			<label for="name" style="float: left;">请填写文件目录:</label>
			<textarea name="content"><?php echo $content; ?></textarea>
		</div>
		<div>
			<label for="botton">请选检查项:</label>
			<select name="botton">
				<option value="make" <?php echo $botton_select == 'make' ? 'selected' : '' ?>>生成目录</option>
				<option value="check" <?php echo $botton_select == 'check' ? 'selected' : '' ?>>检查是否正确</option>
			</select>
		</div>
		<button type="submit">提交</button>
	</form>
</body>
</html>

转载于:https://my.oschina.net/u/3004226/blog/1531330

Logo

更多推荐