获取某段时间内修改的文件及结构
2014-1-14 16:42 Tuesday  

分类: PHP 评论(56) 浏览(118672)

代码如下

<?php
/**
 * 获取某段时间内修改的文件及结构
 *
 * @author aboc QQ:9986584
 * @website http://www.phpec.org
 */
header('Content-Type:text/html;charset=utf-8');
$savepath = 'F:/lastedit/';
if(isset($_POST['find'])){
	set_time_limit(0);
	$savedir = trim($_POST['savedir']);
	$finddir = trim($_POST['finddir']);
	$lastedittime = strtotime(trim($_POST['lastedittime']));
	if($savedir==""){
		$savedir = 'temp';
	}
	if(!is_dir($savepath.$savedir)){
		mkdir($savepath.$savedir,0777,true);
	}
	if( $finddir=="" || !is_dir($finddir)){
		show("寻找的文件夹不能为空或不是文件夹");
	}
	$includedir = trim($_POST['includedir']);
	if($includedir!=""){
		$includedir = nl2br($includedir);
		$includedir = explode('<br />',$includedir);
		$includedir = array_map("trim",$includedir);
		foreach($includedir as $k=>$v){
			if($v==""){
				unset($includedir[$k]);
			}
		}
	} else{
		$includedir = array();
	}
	$excludedir = trim($_POST['excludedir']);
	if($excludedir!=""){
		$excludedir = nl2br($excludedir);
		$excludedir = explode('<br />',$excludedir);
		$excludedir = array_map("trim",$excludedir);
		foreach($excludedir as $k=>$v){
			if($v==""){
				unset($excludedir[$k]);
			}
		}
	} else{
		$excludedir = array();
	}
	$savedir = $savepath.$savedir;
	find_dir($finddir);

}
?>
<form action="" method="post">
寻找的文件夹:<br />
<input type="text" name="finddir" value="<? echo isset($_POST['finddir'])?$_POST['finddir']:'';?>" /><br />
保存的文件夹:<br />
<? echo $savepath;?><input type="text" name="savedir" value="<? echo isset($_POST['savedir'])?$_POST['savedir']:'';?>" /><br />
最后修改时间:<br />
<input type="text" name="lastedittime" value="<? echo isset($_POST['lastedittime'])?$_POST['lastedittime']:'';?>" /><br />
包含文件夹:<br />
<textarea name="includedir" style="width:200px;height:80px;"><? echo isset($_POST['includedir'])?$_POST['includedir']:'';?></textarea><br />
排除文件夹:<br />
<textarea name="excludedir" style="width:200px;height:80px;"><? echo isset($_POST['excludedir'])?$_POST['excludedir']:'';?></textarea><br />
<input type="submit" name="find" value="开始" />
</form>
<?php
function show($str){
	echo '<div>'.$str.'</div>';
	echo '<div><a href="javascript:back(-1)">返回</a></div>';
	exit;
}
function find_dir($dir){
	global $savedir,$finddir,$lastedittime,$includedir,$excludedir;
	$destdir = str_replace($finddir,$savedir,$dir);
	$fp = scandir($dir);
	foreach($fp as $k=>$v){
		if($v == '.' || $v == '..'){
			continue;
		}
		if(is_file($dir.'/'.$v)){
			//文件
			if(filemtime($dir.'/'.$v)>=$lastedittime){
				//有效,写
				if(!is_dir($destdir)){
					mkdir($destdir,0777,true);
				}
				copy($dir.'/'.$v,$destdir.'/'.$v);
				echo $dir.'/'.$v.'=>'.$destdir.'/'.$v.'<br />';
			}
		} else{
			//文件夹
			if($includedir && in_array($v,$includedir)){
					copy_dir($dir.'/'.$v,$destdir.'/'.$v);
					echo $dir.'/'.$v.'=>'.$destdir.'/'.$v.'<br />';
			} else{
				//遍历
				if($excludedir && in_array($v,$excludedir)){
					continue;
				}
				find_dir($dir.'/'.$v);
			}
		}
	}
}
function copy_dir($path,$dest){
	if(is_dir($path)){
		$fp = scandir($path);
		if(!$fp){
			continue;
		}
		if(!is_dir($dest)){
			mkdir($dest,0777,true);
		}
		foreach($fp as $v){
			if($v=='.' || $v=='..'){
				continue;
			}
			if(is_dir($path.'/'.$v)){
				copy_dir($path.'/'.$v,$dest.'/'.$v);
			} else{
				copy($path.'/'.$v,$dest.'/'.$v);
			}
		}
		return true;
	}
	elseif(is_file($path)){
		return copy($path,$dest);
	}
	else{
		return false;
	}
}

至于以上这货的用处,一般用于将某段时间内修改的文件按目录结构找出来,这样就能打包发给别人或者是上传到服务器上了。

Windows 7 +Apache2.2+PHP5.3测试通过

+1 24

留下你的看法: