简体   繁体   中英

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

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

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM