php 在PHP中删除所有超过2天的文件的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8965778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The correct way to delete all files older than 2 days in PHP
提问by user4951
Just curious
只是好奇
$files = glob(cacheme_directory()."*");
foreach($files as $file)
{
$filemtime=filemtime ($file);
if (time()-$filemtime>= 172800)
{
unlink($file);
}
}
I just want to make sure if the code is correct or not. Thanks.
我只想确定代码是否正确。谢谢。
回答by buschtoens
You should add an is_file()
check, because PHP normally lists .
and ..
, as well as sub-directories that could reside in the the directory you're checking.
您应该添加一个is_file()
检查,因为 PHP 通常会列出.
和..
以及可能驻留在您正在检查的目录中的子目录。
Also, as this answer suggests, you should replace the pre-calculated seconds with a more expressive notation.
此外,正如这个答案所暗示的那样,您应该用更具表现力的符号替换预先计算的秒数。
<?php
$files = glob(cacheme_directory()."*");
$now = time();
foreach ($files as $file) {
if (is_file($file)) {
if ($now - filemtime($file) >= 60 * 60 * 24 * 2) { // 2 days
unlink($file);
}
}
}
?>
Alternatively you could also use the DirectoryIterator
, as shown in this answer. In this simple case it doesn't really offer any advantages, but it would be OOP way.
或者,您也可以使用DirectoryIterator
,如本答案所示。在这种简单的情况下,它并没有真正提供任何优势,但它会是 OOP 方式。
回答by Stefanie Janine St?lting
The easiest way is by using DirectoryIterator:
最简单的方法是使用DirectoryIterator:
<?php
if (file_exists($folderName)) {
foreach (new DirectoryIterator($folderName) as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
if ($fileInfo->isFile() && time() - $fileInfo->getCTime() >= 2*24*60*60) {
unlink($fileInfo->getRealPath());
}
}
}
?>
回答by Maksim.T
Another simplier and more modern way, using FilesystemIterator.
另一种更简单、更现代的方法,使用FilesystemIterator。
I'm using 'logs' directory as an example.
我以“logs”目录为例。
$fileSystemIterator = new FilesystemIterator('logs');
$now = time();
foreach ($fileSystemIterator as $file) {
if ($now - $file->getCTime() >= 60 * 60 * 24 * 2) // 2 days
unlink('logs/'.$file->getFilename());
}
Main advantage is: DirectoryIterator returns virtual directories "." and ".." in a loop. But FilesystemIterator ignores them.
主要优点是:DirectoryIterator 返回虚拟目录“.”。和“..”在一个循环中。但是 FilesystemIterator 忽略了它们。
回答by Lachit
/* Delete Cache Files Here */
$dir = "cache/"; /** define the directory **/
/*** cycle through all files in the directory ***/
foreach (glob($dir."*") as $file) {
//foreach (glob($dir.'*.*') as $file){
/*** if file is 24 hours (86400 seconds) old then delete it ***/
if (filemtime($file) < time() - 172800) { // 2 days
unlink($file);
}
}
Hope it help you.
希望对你有帮助。
回答by Niet the Dark Absol
Looks correct to me. I'd just suggest you replace 172800
with 2*24*60*60
for clarity.
对我来说看起来是正确的。我只是建议你更换172800
使用2*24*60*60
的清晰度。
回答by John Carter
回答by user3657553
Here is an example of how to do it recursively.
这是一个如何递归执行的示例。
function remove_files_from_dir_older_than_x_seconds($dir,$seconds = 3600) {
$files = glob(rtrim($dir, '/')."/*");
$now = time();
foreach ($files as $file) {
if (is_file($file)) {
if ($now - filemtime($file) >= $seconds) {
echo "removed $file<br>".PHP_EOL;
unlink($file);
}
} else {
remove_files_from_dir_older_than_x_seconds($file,$seconds);
}
}
}
remove_files_from_dir_older_than_x_seconds(dirname(__file__).'/cache/', (60 * 60 * 24 * 1) ); // 1 day
回答by Leon
I reckon this is much tidier and easier to read and modify.
我认为这更整洁,更易于阅读和修改。
$expire = strtotime('-7 DAYS');
$files = glob($path . '/*');
foreach ($files as $file) {
// Skip anything that is not a file
if (!is_file($file)) {
continue;
}
// Skip any files that have not expired
if (filemtime($file) > $expire) {
continue;
}
unlink($file);
}
回答by Miguel
/** It deletes old files.
* @param string $dir Directory
* @param int $secs Files older than $secs seconds
* @param string $pattern Files matching $pattern
*/
function delete_oldfiles($dir,$secs,$pattern = "/*")
{
$now = time();
foreach(glob("$dir$pattern") as $f) {
if (is_file($f) && ($now - filemtime($f) > $secs)) unlink($f);
}
}