比较两个unix时间文件以删除旧文件

发布于 2024-10-24 18:46:50 字数 1375 浏览 1 评论 0原文

我使用下面的代码来获取云服务器上文件的时间戳,然后删除任何超过一定期限的文件。

目前,代码设置为删除任何早于 10 分钟的内容以进行测试。

然而,即使我上传一些文件然后 10 秒后运行脚本,它也会删除所有内容。

有什么我没有看到的小问题吗?

    $auth->authenticate();

    //connect
    $conn = new CF_Connection($auth);

    //create a handle for the CLOUD FILES container we want to delete from
    $daily_dose = $conn->get_container('daily_dose');

    //delete the obeject 
    $daily_packages = $daily_dose->list_objects();
    //print_r($daily_packages);
    //$public_container = $conn->get_container("public");   

    //$file_age = 86400; // 1 day in seconds
    $file_age = 600; // 10 minutes

    echo 'current time: ';
    echo $current_time = time();
    echo '<br>';
    echo 'time offset: ';
    echo $previous_day = $current_time - $file_age;
    echo '<br>';

    foreach($daily_packages as $package)
    {   
        echo $item = $daily_dose->get_object($package);
        $modified = $item->last_modified;
        echo ' ' . $modified;
        echo ' -> ' . strtotime($modified);
        echo '<br>';

        //If the modified time of the file is less the current time - 1 day in seconds (older than 1 day) delete it
        if ( $modified < $previous_day )
        {
            //delete the file
            $daily_dose->delete_object($item);
        }
    }

I am using the below bit of code to get the timestamps of files on my cloud server and then delete any that are over a certain age.

For now the code is set to delete anything older than 10 minutes for testing.

However it deletes everything, even if I upload some files then run the script 10 seconds later.

Is there some small problem I am not seeing?

    $auth->authenticate();

    //connect
    $conn = new CF_Connection($auth);

    //create a handle for the CLOUD FILES container we want to delete from
    $daily_dose = $conn->get_container('daily_dose');

    //delete the obeject 
    $daily_packages = $daily_dose->list_objects();
    //print_r($daily_packages);
    //$public_container = $conn->get_container("public");   

    //$file_age = 86400; // 1 day in seconds
    $file_age = 600; // 10 minutes

    echo 'current time: ';
    echo $current_time = time();
    echo '<br>';
    echo 'time offset: ';
    echo $previous_day = $current_time - $file_age;
    echo '<br>';

    foreach($daily_packages as $package)
    {   
        echo $item = $daily_dose->get_object($package);
        $modified = $item->last_modified;
        echo ' ' . $modified;
        echo ' -> ' . strtotime($modified);
        echo '<br>';

        //If the modified time of the file is less the current time - 1 day in seconds (older than 1 day) delete it
        if ( $modified < $previous_day )
        {
            //delete the file
            $daily_dose->delete_object($item);
        }
    }

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

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

发布评论

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

评论(1

幸福%小乖 2024-10-31 18:46:50

如果你在 $modified 上调用 strtotime() ,我想它已经不是 UNIX 时间戳了。
time() 确实返回 UNIX 时间戳。

那么,检查不应该如下所示:

if(strtotime($modified) < $previous_day)

这样您就可以比较 UNIX 时间戳,并且考虑到 $item->last_modified 返回正确的值,您的代码应该可以工作。

如果没有,请确保 time() 返回的时间和软件在 $items 上设置的 last_modified 值之间没有时间或时区差异(通过一些调试输出可以轻松检查)

If you are calling strtotime() on $modified, I guess it is not a UNIX timestamp already.
time() does return a UNIX timestamp.

Shouldn't the check, then, be as follows:

if(strtotime($modified) < $previous_day)

That way you are comparing UNIX timestamps and, given that $item->last_modified returns the correct value, your code should work.

If not, make sure there are no time or timezone differences between what time() returns, and the software setting the last_modified value on your $items (easy to check with some debug output)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文