PHP fileatime -> Always 1 o'clock in the morning

658

I wrote a routine to delete all files in a folder that have not been used for the last x days.

I am using the functionfileatime() [or more precisely the DirectoryIterator functiongetATime() ]

When debugging, I noticed that the last access time for all the files was 1 o'clock this morning.

This is the time when the server is automatically backed-up.

So the question is... How to find out the last time the file was accessed, by a user and not the automatic back up.

Any help would be appreciated.

David

47

Answer

Solution:

This is the solution I came up with. Whenever a user accesses a file, I copy the file to a new name and delete the older version. This sets a new modification time.

Independently, in a separate routine, I look through all files in the folder and delete any folders that have not been accessed (modified) since X days.

function DeleteUnusedFiles($FolderName,$NumberOfDays) 
{
$fileSPLObjects =  new RecursiveIteratorIterator( new RecursiveDirectoryIterator($FolderName),RecursiveIteratorIterator::LEAVES_ONLY  );
foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) 
  {
  $TheFileName=$fileSPLObject->getFilename();
  $DaysOld=(int)( time()-$fileSPLObject->getMTime())/(3600*24) ); 
  if( $DaysOld  >= $NumberOfDays ) unlink( $fileSPLObject->getRealPath() );
  }
}

People are also looking for solutions to the problem: Adding Multiple Recipients using Sendgrid marketing Email in PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.