Home > php教程 > PHP源码 > php版 筛选需要的文件

php版 筛选需要的文件

PHP中文网
Release: 2016-05-25 17:09:58
Original
1488 people have browsed it

                       

                       

1. [代码][PHP]代码   

           

<?php
// dir 一定需要用单引号包围,因为目录中可能有特殊字符(如c:\file中\f)
date_default_timezone_set("Asia/Shanghai") ;
$dir = &#39;D:\workspace\xx\src\contents&#39;;

// 目标目录放在当前桌面
$target_dir = &#39;C:\Users\XJ\Desktop\XJ&#39;;
// $target_dir = &#39;C:\Users\当前用户的登录名\Desktop\你想建立的文件夹名字&#39;;
// 查找多久之前的文件
$target_time = strtotime(&#39;-1 hours&#39;);
 
function create_dir($root, $filename, $target_time, $target_dir) {
	if (file_exists($filename)) {
		if (is_dir($filename)) {
			$remove_root = preg_quote($root);
			$new_filename = preg_replace("@$remove_root@", "", $filename);
			@mkdir($target_dir . $new_filename);
			foreach (glob("$filename/*") as $key=>$value) {
				create_dir($root, $value, $target_time, $target_dir);
			}
		} else {
			$modify_time = filemtime($filename);
			// 新增文件使用filectime
			$create_time = filectime($filename);
			if ($modify_time >= $target_time || $create_time >= $target_time) {
				$root = preg_quote($root);
				$str = file_get_contents($filename);
				$filename = preg_replace("@$root@", "", $filename);
				if (!is_file($target_dir . $filename)) {
					file_put_contents($target_dir . $filename,$str);
				}
				echo $filename . "<br/>";
			}
		}
	} else {
		echo &#39;参数错误&#39;;
		return false;
	}
}

// 第二参数控制是全删,还是只是删除空目录
function remove_dir($target_dir, $all_flg = false){
	foreach (glob("$target_dir/*") as $key=>$value) {
		if (is_dir($value) && count(glob("$value/*")) == 0) {
			// 删除第一层目录为空的目录
			rmdir($value);
		} else if (is_dir($value) && count(glob("$value/*")) > 0) {
			// 校验当前目录的所有子目录
			foreach(glob("$value/*") as $sub_key=>$sub_value) {
				remove_dir($sub_value, $all_flg);
			}
			// 经过上一步的处理,再回到当前目录,检验当前目录的子目录是否有空的目录
			remove_dir($value, $all_flg);
			// 如果当前目录经过上面的处理后是空目录,则删除
			if (is_dir($value) && count(glob("$value/*")) == 0) {
				rmdir($value);
			}
		}
		if ($all_flg) {
			if (is_file($value)) {
				unlink($value);
			}
		}
	}
}

// 先清空目标目录
remove_dir($target_dir, true);
// 重建目录
create_dir($dir, $dir, $target_time, $target_dir);
sleep(1);
// 筛选目录
remove_dir($target_dir);
Copy after login

                   

                   

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template