txt - Script php para borrar archivos con más de 24 horas, borra todos los archivos
reemplazar archivo php (6)
- Tú quieres
>lugar. - A menos que estés ejecutando en Windows, quieres
filemtime()lugar.
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);
}
}
}
}
?>
funcionando bien
$path = dirname(__FILE__);
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
$timer = 300;
$filetime = filectime($file)+$timer;
$time = time();
$count = $time-$filetime;
if($count >= 0) {
if (preg_match(''//.png$/i'', $file)) {
unlink($path.''/''.$file);
}
}
}
}
$path = ''/cache/'';
// 86400 = 1day
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ( (integer)(time()-filemtime($path.$file)) > 86400 && $file !== ''.'' && $file !== ''..'') {
unlink($path.$file);
echo "/r/n the file deleted successfully: " . $path.$file;
}
}
}
<?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")
<?php
$dir = getcwd()."/temp/";//dir absolute path
$interval = strtotime(''-24 hours'');//files older than 24hours
foreach (glob($dir."*") as $file)
//delete if older
if (filemtime($file) <= $interval ) unlink($file);?>
(time()-filectime($path.$file)) < 86400
Si la hora actual y la hora cambiada del archivo están dentro de 86400 segundos entre sí, entonces ...
if (preg_match(''//.pdf$/i'', $file)) {
unlink($path.$file);
}
Creo que ese puede ser tu problema. Cámbielo a> o> = y debería funcionar correctamente.