Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
delete files yyyy-mm-dd.pdf more than a month old

I have pdf files that I want to delete based on the file name that aremore than a month old. For example, if (today - 1 month) is '2022-06-01.pdf' then I want to delete files like '2022-04-13.pdf' and '2021-01-22.pdf'. I don't want to base the selection criteria based on actual date of modification or creation.

SAMPLE (in temp/)

2019-02-01.pdf  
2020-07-01.pdf  
2021-03-10.pdf  
2021-08-04.pdf  
2022-03-30.pdf
2019-02-27.pdf  
2020-07-08.pdf  
2021-03-17.pdf  
2021-08-11.pdf  
2022-04-06.pdf

HTML

oldFiles(); purgeOLD();

JS

function oldFiles() { // this is ok
yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 30);

actualYear = yesterday.getFullYear();
actualMonth = yesterday.getMonth() + 1;
actualDay = yesterday.getDate();

if (actualMonth < "10") {actualMonth = "0" + actualMonth;}
if (actualDay < "10") {actualDay = "0" + actualDay;}

backtrack = actualYear +'-' +actualMonth +'-' +actualDay;
}

function purgeOLD () { // is this where the problem is ?
var new_data = backtrack; var data; 
$.ajax({url: 'test.php', type: 'post', async: false, data: {user: new_data}});
}

PHP "test.php"

<?php // or is this ?
$result = $_GET['user'];
$rdm = $result .'.pdf';
$glob = glob("temp/2???-??-??.pdf");

foreach($glob as $file) {
if ($file < $rdm) {
{unlink('/home/example/public_html/temp/' .$file ."'"))}    
}
}

?>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would do this entirely server-side.

  1. Construct a DateTime instance for "1 month ago"
  2. Create a filename from that instance
  3. Iterate the files and compare the basename (ignoring any directory prefix)
  4. If the filename represents a date prior to "1 month ago", delete it
$lastMonth = new DateTimeImmutable('-1 month');
$cutoff = sprintf('%s.pdf', $lastMonth->format('Y-m-d'));

$glob = glob("temp/2???-??-??.pdf");

foreach($glob as $file) {
    if (basename($file) < $cutoff) {
        unlink($file);
    }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda