合并两个基本上做同样事情的函数

发布于 2024-10-14 06:50:43 字数 833 浏览 2 评论 0原文

我目前正在使用两个函数在一分钟后从每个文件夹中删除,但它们基本上做同样的事情(只是调用不同的文件夹)。我想知道是否可以将它们合并为一个?

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 技术交流群。

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

发布评论

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

评论(4

苍暮颜 2024-10-21 06:50:43

将文件夹名称作为参数传递。

function DeleteFromFolder($captchaFolder) {
    $fileTypes     = '*.jpg';
    $expire_time   = 1; 
    foreach(glob($captchaFolder . $fileTypes) as $Filename) {
     $FileCreationTime = filectime($Filename);
     $FileAge = time() - $FileCreationTime; 

if($FileAge > ($expire_time * 60))
   {
    unlink($Filename);
          }
       }
    }

Pass the folder name as an argument.

function DeleteFromFolder($captchaFolder) {
    $fileTypes     = '*.jpg';
    $expire_time   = 1; 
    foreach(glob($captchaFolder . $fileTypes) as $Filename) {
     $FileCreationTime = filectime($Filename);
     $FileAge = time() - $FileCreationTime; 

if($FileAge > ($expire_time * 60))
   {
    unlink($Filename);
          }
       }
    }
心碎无痕… 2024-10-21 06:50:43

感谢大家的回答,但我现在通过添加以下内容对其进行排序:

unlink(path/to/temp/image.jpg);

到我的结果页面,一旦创建拇指并删除与其关联的功能,就会删除上传的图像。

再次感谢您的回答:)

Thanks for your answers everyone but I have now sorted it by adding:

unlink(path/to/temp/image.jpg);

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 :)

美煞众生 2024-10-21 06:50:43
function DeleteFromFolder($captchaFolder) {
    $fileTypes     = '*.jpg';
    // and so on
}

如果愿意,您可以添加两个辅助函数

function DeleteFromFolder1() { return DeleteFromFolder('folder1/'); }
function DeleteFromFolder2() { return DeleteFromFolder('folder2/'); }
function DeleteFromFolder($captchaFolder) {
    $fileTypes     = '*.jpg';
    // and so on
}

If you like, you can add two helper functions

function DeleteFromFolder1() { return DeleteFromFolder('folder1/'); }
function DeleteFromFolder2() { return DeleteFromFolder('folder2/'); }
迷乱花海 2024-10-21 06:50:43

要尝试在不编辑结构的情况下执行此操作,您可以将变量传递到主函数中。您可以执行类似这样的操作

    function DeleteFromFolder1($dir=NULL) {
        if($dir == NULL)
             $captchaFolder = 'folder1/';
        else
             $captchaFolder = $dir;
        $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() {
    DeleteFromFolder1("folder2/");
}
}

,无需对当前代码库进行任何重大更改即可工作。

编辑(为了清晰起见,添加更多描述)

我假设您的代码已经以某种方式实现。如果是这种情况,一个相当笨重的解决方案就像我上面的那样(这将允许进行最少数量的编辑)。否则,您可以将此功能合并到第一个功能中,它将正常工作。 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

    function DeleteFromFolder1($dir=NULL) {
        if($dir == NULL)
             $captchaFolder = 'folder1/';
        else
             $captchaFolder = $dir;
        $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() {
    DeleteFromFolder1("folder2/");
}
}

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.

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