删除文件夹内除最后一个之外的所有文件?

发布于 2025-01-01 08:06:30 字数 1045 浏览 0 评论 0原文

我正在寻找一个脚本来循环遍历文件夹并删除其中的所有文件,但最后一个、最近的文件(我已将每个文件的名称标记为 filename_date('Y')_date('m' )_date('d').extension),不确定是否相关)。

我在堆栈上找到了这个脚本:

if ($handle = opendir('/path/to/your/folder')) 
{
    $files = array();
    while (false !== ($file = readdir($handle))) 
    {
        if (!is_dir($file))
        {
            // You'll want to check the return value here rather than just blindly adding to the array
            $files[$file] = filemtime($file);
        }
    }

    // Now sort by timestamp (just an integer) from oldest to newest
    asort($files, SORT_NUMERIC);

    // Loop over all but the 5 newest files and delete them
    // Only need the array keys (filenames) since we don't care about timestamps now as the array will be in order
    $files = array_keys($files);
    for ($i = 0; $i < (count($files) - 5); $i++)
    {
        // You'll probably want to check the return value of this too
        unlink($files[$i]);
    }
}

上面删除了除最后五个之外的所有内容。这是一个好方法吗?或者还有另一种方法,更简单或更好的方法?

I'm looking for a script to loop through a folder and delete all the files inside it but the last, most recent one ( I have marked the name of each file as filename_date('Y')_date('m')_date('d').extension), not sure if relevant ).

I have found this script here on stack:

if ($handle = opendir('/path/to/your/folder')) 
{
    $files = array();
    while (false !== ($file = readdir($handle))) 
    {
        if (!is_dir($file))
        {
            // You'll want to check the return value here rather than just blindly adding to the array
            $files[$file] = filemtime($file);
        }
    }

    // Now sort by timestamp (just an integer) from oldest to newest
    asort($files, SORT_NUMERIC);

    // Loop over all but the 5 newest files and delete them
    // Only need the array keys (filenames) since we don't care about timestamps now as the array will be in order
    $files = array_keys($files);
    for ($i = 0; $i < (count($files) - 5); $i++)
    {
        // You'll probably want to check the return value of this too
        unlink($files[$i]);
    }
}

This above deletes anything but the last five. Is this a good way to do it ? Or is there another way, simpler or better one ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷子口的你 2025-01-08 08:06:30

那行得通。我不相信有更简单的方法可以做到这一点。另外,您的解决方案实际上非常简单。

That works. I don't believe there's a simpler way to do it. Plus, your solution is actually quite simple.

恬淡成诗 2025-01-08 08:06:30

我认为这是一个很好的解决方案。只需修改循环

,但您可以避免 for 循环以下降模式对数组进行排序,这样您就可以删除数组的所有其余部分,仅保存第一个文件
编辑
从最新到旧排序

i think is a good solution. just modify the loop

but you could avoid for loop sorting array in descend mode so you could delete all the rest of array saving just the first file
EDIT
sort from newest to older

撩发小公举 2025-01-08 08:06:30

我知道这很旧,但你也可以这样做

$directory = array_diff(scandir(pathere), array('..', '.'));
$files = [];
foreach ($directory as $key => $file) {
    $file = pathere.$file;
    if (file_exists($file)) {
        $name = end(explode('/', $file));
        $timestamp = preg_replace('/[^0-9]/', '', $name);
        $files[$timestamp] = $file;
    }
}
// unset last file
unset( $files[max(array_keys($files))] );
// delete old files
foreach ($files as $key => $dfiles) {
    if (file_exists($dfiles)) {
        unlink($dfiles);
    }
}

i know this is old but you could also do it like this

$directory = array_diff(scandir(pathere), array('..', '.'));
$files = [];
foreach ($directory as $key => $file) {
    $file = pathere.$file;
    if (file_exists($file)) {
        $name = end(explode('/', $file));
        $timestamp = preg_replace('/[^0-9]/', '', $name);
        $files[$timestamp] = $file;
    }
}
// unset last file
unset( $files[max(array_keys($files))] );
// delete old files
foreach ($files as $key => $dfiles) {
    if (file_exists($dfiles)) {
        unlink($dfiles);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文