首页 php教程 PHP源码 php版 筛选需要的文件

php版 筛选需要的文件

May 25, 2016 pm 05:09 PM

                       

                       

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);
登录后复制

                   

                   

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)