在php中删除目录中的文件我们需要遍历目录中的文件,然后获取文件的时间,我们再进行判断这个文件是不是过期了,如果适合条件就可以删除了。
删除文件
代码如下 |
复制代码 |
file_delete = "home/meeta/my.php"; if (unlink($file_delete)) { echo "The file was deleted successfully.", "n"; } else { echo "The specified file could not be deleted. Please try again.", "n"; } ?> |
下面加了判断文件是否存在
代码如下 |
复制代码 |
$myfile = "./test1.txt"; if (file_exists($myfile)) { $result=unlink ($myfile); echo $result; } ?> |
删除指定文件夹根目录指定日期之前文件
代码如下 |
复制代码 |
<?php function del_dir($dir){ //删除目录 if(!($mydir=@dir($dir))){ return; } while($file=$mydir->read()){ if(is_dir("$dir$file") && $file!='.' && $file!='..'){ @chmod("$dir$file", 0777); del_dir("$dir$file"); }elseif(is_file("$dir/$file")){ $file_time=@stat($file); //读取文件的最后更新时间 if(time()-$file_time>3600*24*14){ @chmod("$dir/$file", 0777); @unlink("$dir/$file"); } } } $mydir->close(); @chmod($dir, 0777); @rmdir($dir); } ?> |
本文地址:http://www.phprm.com/code/47164.html
转载随意,但请附上文章地址:-)