How to automatically delete file older than 24 hours of a .pdf format in a specific folder with PHP

1k views Asked by At

I keep getting many pdf file in my directory root/files/pdfs. I want a PHP script to automatically delete only the .pdf files from the pdfs folder that are older than 24 hours (86400 seconds).

What permissions will the .php file require? Where to put the file? Should I have to run the PHP by visiting the link of the PHP file?

NOTE: I have a FTP access to a subdomain

1

There are 1 answers

0
nodeffect On BEST ANSWER

You can try this method, it checks the file creation time. Or use "filemtime" for file modified time.

$dir = "root/files/pdfs/";   //your folder location

foreach (glob($dir."*.pdf") as $file) { 
    if (filectime($file) < time() - 86400) { 
        unlink($file);
    }
}