Delete automatic files

Good Guys, I have a system, where practically daily are sent via uploads pdf files. After a while you get a lot of accumulated file.

For example: Upo 100 pdf files hj, tomorrow I already put 20. So, from the day I uploaded the 100 pdfs files, after 3 months deletes these 100.. the next day the same scheme and erases the 20 and so on, but it does not erase all at once.

Any ideas or help?

Thank you right now!

I have this programming in PHP below,

    <?php
  $dateFormat = "d-m-Y H:i:s";
  $dir = 'uploads/*/*';

  if(isset($_POST['excluir'])){
    if($objs == glob($dir)) {
    foreach($objs as $obj) {
      if (is_dir($obj)) continue; // Verifica se o arquivo é um diretório, se for, tudo que está abaixo é desconsiderado

      $dateFile = strtotime(date($dateFormat, filemtime($obj))); // Data da última modificação do arquivo convertida em time;
      $dateToRemove = strtotime(date($dateFormat, filemtime($obj) + (60 * 5))); // Tempo da última modificação + 3 meses convertida em time;
      if($dateFile >= $dateToRemove) unlink($obj); // Exclui o arquivo se este possuir 03 meses desde a última modificação
    }
}
}
?> 

Button:

<button class="btn btn-danger" name="excluir" onclick="return confirm('Tem certeza que deseja apagar todos os Boletos?                         ATENÇÃO! ESSA AÇÃO NÃO TERÁ VOLTA!')" >APAGAR TODOS OS BOLETOS</button>

EDIT: Code edited as explained. I did tests with 1 minute, I Up Files, I waited 1 minute.. upei a few more, went to delete and deleted all.

 4
Author: JeanTDZ, 2019-05-27

3 answers

Hello! I noticed that there is a syntax error in your code:

If (OBJ objs = glob (d dir)) {

Or correct:

if($objs == glob($dir)){

In my view, the explanation of your problem was not very clear. Could you give more details of your problem?

@ EDIT-28/05

From Keven Carneiro's answer I adapted the script in a way that I believe will solve your problem.

$dateFormat = "d-m-Y";
$dir = 'uploads/*/*';

if($objs == glob($dir)) {
    foreach($objs as $obj) {
      if (is_dir($obj)) continue; // Verifica se o arquivo é um diretório, se for, tudo que está abaixo é desconsiderado

      $dateFile = strtotime(date($dateFormat, filemtime($obj))); // Data da última modificação do arquivo convertida em time;
      $dateToRemove = strtotime(date($dateFormat, filemtime($obj) + (3600 * 24 * 90))); // Tempo da última modificação + 3 meses convertida em time;
      if(time() >= $dateToRemove) unlink($obj); // Exclui o arquivo se este possuir 03 meses desde a última modificação
    }
}

If you want to test the difference in hours you should:

  1. change the "dat dateFormat" to "d-M-Y H: I: s";
  2. change the next row by changing the sum time to, for example, 3600 only. (One hour value)

Dat dateToRemove = strtotime (data (dat dateFormat, filemtime(OB obj) + (3600 * 24 * 90)));

Make sure to check if the value being received in OBJ objs is valid to enter the first condition:

If (OBJ objs == glob (d dir)) {

I tried explain to the maximum what each function does, but you can ask questions here or consult the documentation! I hope I helped.

 2
Author: Rohan, 2019-05-28 18:43:46

You can make a script in PHP that reads the creation date of a file to remove it using the function filectime:

$dateFormat = "Y-m-d"; // Altere para "Y-m-d H:i:s" se quiser levar em consideração os segundos.
$dateToRemove = date($dateFormat, strtotime("-3 months"));
$dir = 'uploads/*/*';

if($objs = glob($dir)) {
    foreach($objs as $obj) {
      if (is_dir($obj)) continue;
      if (date($dateFormat, filectime($obj)) > $dateToRemove) continue;
      unlink($obj);
    }
}

You can save this code to a file .php and run it from time to time. In order not to have to run manually, you can use a tool that runs scheduled scripts at time intervals defined by you, in Linux scripts of this type are known as CRON and in Windows this functionality is known as scheduled tasks, available through the Task Scheduler.

You can set it up manually or use an online service like https://cron-job.org , which will call your PHP page as often as you set.

For more details about the function filectime you can consult the PHP documentation: https://www.php.net/manual/pt_BR/function.filectime.php


PS: you may have strange my way of writing code if (condicaoFalsa) continue, but it is a technique used to increase code maintainability.

You can see an example of this here: https://softwareengineering.stackexchange.com/questions/47789/how-would-you-refactor-nested-if-statements

 3
Author: Keven Carneiro, 2019-05-29 15:06:26

Here to work I changed the date format to: $dateFormat = "Y-m-d";

 1
Author: Marcio Souza, 2019-05-27 13:54:54