title: Use a PHP script and cron to delete files in a directory over x days old
body: <p>I have looked around this site and others to find a simple php script that I can use with cron to remove files over X days old in a directory.  There seem to be plenty but none work for me.  I am on a shared server (G C Solutions) and the hosters are great but the packageI am on does not include shell access so I don't think I can use .sh or bash commands. </p>
  
  <p>I have a neat php script to do a Mysqldump of my database, copy it to a directory in my home area ( /home/mysite/backups ) and send me a copy via email - this all works fine.  Now I was trying to run a script that would just leave 5 days worth of backups in the backups diretory. I am trying this script at present:-</p>
  
  <pre><code>&lt;?php
  
  
  $dir = opendir('/home/mysite/backups");
  if ($dir) {
  
          // Read directory contents
      while (false !== ($file = readdir($dir))) {
  
          // Check the create time of each file (older than 5 days)
          if (filemtime($file) &lt; (time() - 60*60*24*5)) {
              unlink($file);
          }
      }
  }
  ?&gt;
  </code></pre>
  
  <p>It doesn't work, my cron setting string is :- 
  php -q /home/mysite/public_html/scripts/delold6.php</p>
  
  <p>I've tried running it from above the the html_public, no joy, the backup directory rights are set to 755, when my backup script copies the dump to this directory files are set at 644.  I have tried chmoding these to 777 - no joy.  Can anyone help here.  *find /path/to/your_directory -mtime +5 -exec rm -f {} \;* does not work from cron either.</p>