Escribí este script php para eliminar archivos antiguos de más de 24 horas, pero eliminó todos los archivos, incluidos los más nuevos:
<?php
$path = 'ftmp/';
if ($handle = opendir($path)) {
while (false!== ($file = readdir($handle))) {
if ((time()-filectime($path.$file)) < 86400) {
if (preg_match('/\.pdf$/i', $file)) {
unlink($path.$file);
}
}
}
}
?>
Solución del problema
<?php
/** define the directory **/
$dir = "images/temp/";
/*** cycle through all files in the directory ***/
foreach (glob($dir."*") as $file) {
/*** if file is 24 hours (86400 seconds) old then delete it ***/
if(time() - filectime($file) > 86400){
unlink($file);
}
}
?>
También puede especificar el tipo de archivo agregando una extensión después del * (comodín), por ejemplo
Para imágenes jpg use:glob($dir."*.jpg")
Para archivos txt use:glob($dir."*.txt")
Para archivos htm use:glob($dir."*.htm")
No hay comentarios.:
Publicar un comentario