I try to create a php cron job with help of the WP Control plugin. The purpose is to delete all files in the uploads directory for the current year and month.

I use this code:

$files = glob('./wp-content/uploads/' . date('Y') . '/' . date ('m') . '/*'); // get all file names
foreach($files as $file){ // iterate files
chown ($files, 666); //set the permissions
  unlink($file); // delete file

It is not working. When I set the first line of code:

$files = glob('./wp-content/uploads/' . date('Y') . '/' . date ('m') . '/*'); // get all file names


$files = glob('./wp-content/uploads/2019/01/*'); // get all file names`

it works.

What am I missing here?

  • Are you sure that this line was the only change and did you checked what the date function returns? As far as i can see, the two lines should do the same, assuming that the date settings of the server are set correct and we are in January 2019. – ahendwh2 Jan 21 at 15:25
  • Yeah, nothing else is changed. I did put this first line of code in a plugin file and used print_r to see the contents and it is listed fine. All files are present. It only will not work in a php cron job for whatever reason. – A3O Jan 21 at 15:42
  • BTW if you know a better aproach to delete the uploaded images once an hour I welcome you to share it with me. – A3O Jan 21 at 15:46
  • stackoverflow.com/questions/18213108/… I'm guessing it's something similar to this – mrben522 Jan 21 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.