Eliminar archivos que tienen el mismo prefijo
$prefix = 'something_prefix';
unlink($prefix.'.*');
El código anterior no funciona, pero veo que un código como este a continuación funciona bien
unlink('*.jpg');
¿Por qué? Me pregunto si esto va a funcionar.
unlink('*.*');
Cómo eliminar los archivos que comienzan con la misma cadena? así
same123.jpg
sametoo.png
samexxx.gif
Todos comienzan con la cadena "same" pero terminan con una extensión diferente, ¿cómo hacer esto?
Ya tengo una manera barata de hacer esto, pero me pregunto si hay alguna solución mejor?
24
2 answers
Prueba este código:
$mask = 'your_prefix_*.*';
array_map('unlink', glob($mask));
P. s. glob()
requiere PHP 4.3.0 +
50
Author: Raptor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-18 03:18:12
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-18 03:18:12
Puede usar glob para esto. Algo como esto (no lo probé):
foreach (glob("something_prefix*.*") as $filename) {
unlink($filename);
}
23
Author: sectus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-18 03:22:17
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-18 03:22:17