filemtime “warning stat failed for”

前端 未结 4 724
挽巷
挽巷 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 10:55

    in my case it was not related to the path or filename. If filemtime(), fileatime() or filectime() don't work, try stat().

    $filedate = date_create(date("Y-m-d", filectime($file)));
    

    becomes

    $stat = stat($directory.$file);
    $filedate = date_create(date("Y-m-d", $stat['ctime']));
    

    that worked for me.

    Complete snippet for deleting files by number of days:

    $directory = $_SERVER['DOCUMENT_ROOT'].'/directory/';
    $files = array_slice(scandir($directory), 2);
    foreach($files as $file)
    {
        $extension      = substr($file, -3, 3); 
        if ($extension == 'jpg') // in case you only want specific files deleted
        {
            $stat = stat($directory.$file);
            $filedate = date_create(date("Y-m-d", $stat['ctime']));
            $today = date_create(date("Y-m-d"));
            $days = date_diff($filedate, $today, true);
            if ($days->days > 1) 
            { 
                unlink($directory.$file);
            }
        } 
    }
    

提交回复
热议问题