note 36751 added to function.unlink
| From: | php-general at lists dot php dot net | Date: | Tue, 21 Oct 2003 13:02:17 +0000 |
| Subject: | note 36751 added to function.unlink | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-59032@lists.php.net to get a copy of this message | ||
He añadido un par de líneas que comprueban si hay ficheros en el directorio, para que no de error
al ejecutarse.
/*
* @Name: unlink_wc($dir,$pattern)
* @Description: Función para borrar ficheros de un directorio que suporta caracteres comodín( *
and ? )
* @Author: Pablo Rosciani [ pabloATnkstudiosDOTnet ] [ http://pi.nks.com.ar ]
* @Date: 15/10/03
* @param string $dir Directorio donde buscar los ficheros
* @param string $pattern Patrón de busqueda.
* @mofidied: Pedro Gutiérrez
* @date:21/10/2003
* Ej. unlink_wc("/path/to/folder/","*.*");
*/
function unlink_wc($dir, $pattern){
if ($dh = opendir($dir)) {
$cont=0;
//Lista y pone en un array todos los ficheros
while (false !== ($file = readdir($dh))){
if ($file != "." && $file != "..") {
$files[] = $file;
$cont++;
}
}
closedir($dh);
if ($cont){
//Separa el nombre y la extensión del fichero
if(strpos($pattern,".")) {
$baseexp=substr($pattern,0,strpos($pattern,"."));
$typeexp=substr($pattern,strpos($pattern,".")+1,strlen($pattern));
}else{
$baseexp=$pattern;
$typeexp="";
}
//Escapa todos los caracteres regexp
$baseexp=preg_quote($baseexp);
$typeexp=preg_quote($typeexp);
// Permitir ? y *
$baseexp=str_replace(array("\*","\?"),
array(".*","."), $baseexp);
$typeexp=str_replace(array("\*","\?"),
array(".*","."), $typeexp);
//Buscar resultados con el patrón
$i=0;
foreach($files as $file) {
$filename=basename($file);
if(strpos($filename,".")) {
$base=substr($filename,0,strpos($filename,"."));
$type=substr($filename,strpos($filename,".")+1,strlen($filename));
}else{
$base=$filename;
$type="";
}
if(preg_match("/^".$baseexp."$/i",$base) &&
preg_match("/^".$typeexp."$/i",$type)) {
$matches[$i]=$file;
$i++;
}
}
while(list($idx,$val) = each($matches)){
if (substr($dir,-1) == "/"){
unlink($dir.$val);
}else{
unlink($dir."/".$val);
}
}
}
}
----
Manual Page -- http://www.php.net/manual/en/function.unlink.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36751
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36751&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36751&report=yes
Search -- http://master.php.net/manage/user-notes.php