note 38236 deleted from function.unlink by aidan

From: Date: Wed, 08 Sep 2004 11:39:11 +0000
Subject: note 38236 deleted from function.unlink by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-76225@lists.php.net to get a copy of this message
Note Submitter: wmon at terra dot com dot br ---- i have add a new line to the code. this will prevent an error if no files found. 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); } } } } }

« previous php.notes (#76225) next »