doucai1901 2013-04-26 09:28
浏览 48

在PHP中每24小时删除一个文件夹的所有文件

I searched some related questions but unable to understand them.

I want to delete all the files of folder after every 24 hours but sometimes I get warnings and sometime it works. The folder path is c:\wamp\www\Jamil.

Here's the source:

<?php
$dir = "Jamil"; // directory name

foreach (scandir($dir) as $item) {
    if ($item == '.' || $item == '..')
        continue;

        if((time() - filemtime($item)) >= 30    && is_file($item)){
        unlink($dir.DIRECTORY_SEPARATOR.$item);
        echo "All files deleted";}
    }   
//rmdir($dir);

?>

I copied the code from various sites. Can anyone help? I get this warning:

Warning: filemtime() [function.filemtime]: stat failed for jamil.html in C:\wamp\www\delete1.php on line 10

  • 写回答

3条回答 默认 最新

  • dongxie3352 2013-04-26 09:31
    关注

    Try this function;

    function wipedir($dir) {
            try{  
                if(is_dir($dir)){
                    $mydir = opendir($dir);
                    while(false !== ($file = readdir($mydir))) {
                        if($file != "." && $file != "..") {
                            chmod($dir.$file, 0777);
                            if(is_dir($dir.$file)) {
                                chdir('.');
                                destroy($dir.$file.'/');
                                rmdir($dir.$file) or DIE("Unable to delete $dir$file");
                            }else{
                                unlink($dir.$file) or DIE("Unable to delete $dir$file");
                            }
                        }
                    }
                    closedir($mydir);
    
                    return true;
                }else{return true;}
            }catch (Exception $e){return false;} 
        }
    

    So usage would be

    $dir = 'Jamil';
    wipedir($dir);
    

    This should be placed in a file run by crontab etc at the specified interval.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端