filemtime “warning stat failed for”

前端 未结 4 731
挽巷
挽巷 2021-01-01 10:07

I already read it so many questions and answers about it but I can\'t still solve my problem...

I\'m trying to create a function that deletes all the files with \"xm

4条回答
  •  梦谈多话
    2021-01-01 11:03

    I think the problem is the realpath of the file. For example your script is working on './', your file is inside the directory './xml'. So better check if the file exists or not, before you get filemtime or unlink it:

      function deleteOldFiles(){
        if ($handle = opendir('./xml')) {
            while (false !== ($file = readdir($handle))) { 
    
                if(preg_match("/^.*\.(xml|xsl)$/i", $file)){
                  $fpath = 'xml/'.$file;
                  if (file_exists($fpath)) {
                    $filelastmodified = filemtime($fpath);
    
                    if ( (time() - $filelastmodified ) > 24*3600){
                        unlink($fpath);
                    }
                  }
                }
            }
            closedir($handle); 
        }
      }
    

提交回复
热议问题