note 45467 added to function.unlink
| From: | php-general at lists dot php dot net | Date: | Wed, 08 Sep 2004 11:40:49 +0000 |
| Subject: | note 45467 added to function.unlink | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76228@lists.php.net to get a copy of this message | ||
To delete files from a directory matching against a pattern, use the below function.
<?php
function unlink_wc($dir, $pattern)
{
if ($dh = opendir($dir)) {
while (false !== ($file = readdir($dh))){
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
closedir($dh);
if(strpos($pattern,".")) {
$baseexp=substr($pattern,0,strpos($pattern,"."));
$typeexp=substr($pattern,strpos($pattern,".")+1,strlen($pattern));
}else{
$baseexp=$pattern;
$typeexp="";
}
$baseexp=preg_quote($baseexp);
$typeexp=preg_quote($typeexp);
$baseexp=str_replace(array("\*","\?"),
array(".*","."), $baseexp);
$typeexp=str_replace(array("\*","\?"),
array(".*","."), $typeexp);
$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++;
}
}
if (count($matches) > 0){
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+45467
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45467&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45467&report=yes
Search -- http://master.php.net/manage/user-notes.php