note 36583 deleted from function.unlink by aidan
| From: | aidan@php.net | Date: | Wed, 08 Sep 2004 11:39:13 +0000 |
| Subject: | note 36583 deleted from function.unlink by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76226@lists.php.net to get a copy of this message | ||
Note Submitter: pablorATnkstudiosDOTnet - pi.nks.com.ar
----
Hi!
I made a unlink() function but with esteroids, now you can delete files using wildcards like *
and/or ?
<?
/*
* @Name: unlink_wc($dir,$pattern)
* @Description: A unlink function that support wildcards ( * and ? )
* (based on code taked from php.net manual comments)
* @Author: Pablo Rosciani [ pabloATnkstudiosDOTnet ] [ http://pi.nks.com.ar ]
* @Date: 15/10/03
* @param string $dir Directory to search files
* @param string $pattern Pattern to find.
*
*/
function unlink_wc($dir, $pattern){
if ($dh = opendir($dir)) {
//List and put into an array all files
while (false !== ($file = readdir($dh))){
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
closedir($dh);
//Split file name and extenssion
if(strpos($pattern,".")) {
$baseexp=substr($pattern,0,strpos($pattern,"."));
$typeexp=substr($pattern,strpos($pattern,".")+1,strlen($pattern));
}else{
$baseexp=$pattern;
$typeexp="";
}
//Escape all regexp Characters
$baseexp=preg_quote($baseexp);
$typeexp=preg_quote($typeexp);
// Allow ? and *
$baseexp=str_replace(array("\*","\?"), array(".*","."),
$baseexp);
$typeexp=str_replace(array("\*","\?"), array(".*","."),
$typeexp);
//Search for pattern match
$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);
}
}
}
}
unlink_wc("/path/to/folder/","*.*");
?>