合并两个基本上做同样事情的函数
我目前正在使用两个函数在一分钟后从每个文件夹中删除,但它们基本上做同样的事情(只是调用不同的文件夹)。我想知道是否可以将它们合并为一个?
function DeleteFromFolder1() {
$captchaFolder = 'folder1/';
$fileTypes = '*.jpg';
$expire_time = 1;
foreach(glob($captchaFolder . $fileTypes) as $Filename) {
$FileCreationTime = filectime($Filename);
$FileAge = time() - $FileCreationTime;
if($FileAge > ($expire_time * 60))
{
unlink($Filename);
}
}
}
function DeleteFromFolder2() {
$captchaFolder = 'folder2/';
$fileTypes = '*.jpg';
$expire_time = 1;
foreach(glob($captchaFolder . $fileTypes) as $Filename) {
$FileCreationTime = filectime($Filename);
$FileAge = time() - $FileCreationTime;
if($FileAge > ($expire_time * 60))
{
to ($Filename);
}
}
}
I'm currently using two functions to delete from each folder after 1 minute but as they basically do the same thing (just different folders called). I was wondering if they could be merged into one?
function DeleteFromFolder1() {
$captchaFolder = 'folder1/';
$fileTypes = '*.jpg';
$expire_time = 1;
foreach(glob($captchaFolder . $fileTypes) as $Filename) {
$FileCreationTime = filectime($Filename);
$FileAge = time() - $FileCreationTime;
if($FileAge > ($expire_time * 60))
{
unlink($Filename);
}
}
}
function DeleteFromFolder2() {
$captchaFolder = 'folder2/';
$fileTypes = '*.jpg';
$expire_time = 1;
foreach(glob($captchaFolder . $fileTypes) as $Filename) {
$FileCreationTime = filectime($Filename);
$FileAge = time() - $FileCreationTime;
if($FileAge > ($expire_time * 60))
{
to ($Filename);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将文件夹名称作为参数传递。
Pass the folder name as an argument.
感谢大家的回答,但我现在通过添加以下内容对其进行排序:
到我的结果页面,一旦创建拇指并删除与其关联的功能,就会删除上传的图像。
再次感谢您的回答:)
Thanks for your answers everyone but I have now sorted it by adding:
to my results page which deletes the uploaded image once the thumb is created and removed the function associated with it.
Once again thanks for your answers :)
如果愿意,您可以添加两个辅助函数
If you like, you can add two helper functions
要尝试在不编辑结构的情况下执行此操作,您可以将变量传递到主函数中。您可以执行类似这样的操作
,无需对当前代码库进行任何重大更改即可工作。
编辑(为了清晰起见,添加更多描述)
我假设您的代码已经以某种方式实现。如果是这种情况,一个相当笨重的解决方案就像我上面的那样(这将允许进行最少数量的编辑)。否则,您可以将此功能合并到第一个功能中,它将正常工作。 DeleteFromFolder2() 只是一个重定向函数。
该函数采用一个参数
$file
,如果在调用函数时未声明,则该参数为 null。如果$file == NULL
,则默认删除folder1,否则将尝试删除指定的文件夹。我希望事情能澄清一点!祝你好运!
丹尼斯·M.
To try to do this without editing your structure you can pass a variable into your main function. You can do something like this
That should work without making any major changes to your current code base.
EDIT (Adding more of a description for some clarity)
I was assuming that your code was already implemented in some way. If that is the case, a rather clunky solution is like mine above (this will allow the smallest number of edits to be made). Otherwise, you could consolidate this function to just the first one and it will work fine. DeleteFromFolder2() is merely a redirect function.
The function takes an argument
$file
which is null if not declared when calling the function. If$file == NULL
, then it will delete folder1 by default, otherwise, it will attempt to delete the folder specified. I hope that clears things up a bit!Good luck!
Dennis M.