根据日期消除文件

I am trying to eliminate files from my server depending on the time they have been on it. For the moment I have got the following script, but no files are being delated even though they were uploaded on July:

$dir = opendir('uploads/tradsp/');
while($f = readdir($dir))
{

if((time()-filemtime('uploads/tradsp/'.$f) > 3600*24*4) and !(is_dir('uploads/tradsp/'.$f)))
unlink('uploads/tradsp/'.$f);
}
closedir($dir);

Do somebody find a mistake in the code?

thank you for your time

Hi There may be some error in your if condition if you want to delete files of past months you can simply get the month of files and compare that with current month like this

$dir = opendir('uploads/tradsp/');
$filemonth = date("n",filemtime("uploads/tradsp/".$f));
$currentmonth = date('n');
while($f = readdir($dir))
{
if(($filemonth < $currentmonth) and !(is_dir('uploads/tradsp/'.$f))){
unlink('uploads/tradsp/'.$f);
}
}
closedir($dir);

This will remove all the files of lastmonth if you want to remove July month files you can also use condition $filemonth == 7 in if statements