比较两个unix时间文件以删除旧文件
我使用下面的代码来获取云服务器上文件的时间戳
,然后删除任何超过一定期限的文件。
目前,代码设置为删除任何早于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你在 $modified 上调用 strtotime() ,我想它已经不是 UNIX 时间戳了。
time() 确实返回 UNIX 时间戳。
那么,检查不应该如下所示:
这样您就可以比较 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:
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)