0

I am having issue with automatically deleting files from a specific folder on my server.

I need to run an automatic delete every 31 mins on a folder which stores incoming documents. These files will always be *.pdf format.

I have found an issue similar on this site.

How to delete files from directory based on creation date in php?

However my issue is with *.pdf files and I have never used php before, ideally I was looking for a .bat file, but if that's not possible it's no problem.

Community
  • 1
  • 1

1 Answers1

0
<?php
if ($handle = opendir('/path/to/files')) {

    while (false !== ($file = readdir($handle))) { 
        $filelastmodified = filemtime($file);

        if((time() - $filelastmodified) > 24*3600 && strtolower(substr($file, -4)) == ".pdf")
        {
           unlink($file);
        }

    }

    closedir($handle); 
}
?>

This added condition checks if the filename ends with ".pdf". You could run this as a cronjob.

You might as well use shell commands instead. find with -name, -exec and -mtime does the same and saves the need for a PHP parser.

Ulrich Thomas Gabor
  • 6,584
  • 4
  • 27
  • 41
  • Thanks for the help, just to double check, I need to change the path to whatever. Change the time to 1860 in my case. And then to active the commands do I just but it in a *.txt then change the name to *.Crontab? – user3432138 Mar 19 '14 at 08:09
  • Yes, you should run the script by hand to make sure it works. Is your machine a linux at all? If not, you cannot use cronjobs in this way. You must google for an alternative on Windows. If you use linux, please read something up on cronjobs. Just renaming the file does not work. Basically you have to execute `crontab -e` and insert one line into the file in a specific syntax (which is explained e.g. in the linked article concerning cronjobs). – Ulrich Thomas Gabor Mar 19 '14 at 08:14
  • No unfortunately I run on Windows. Sorry this is all new to me, so I'm probably asking a few daft questions. Quick search has sent me to the Task Scheduler, thanks for the help. – user3432138 Mar 19 '14 at 09:22