note 36583 added to function.unlink

From: Date: Wed, 15 Oct 2003 14:54:40 +0000
Subject: note 36583 added to function.unlink
Groups: php.notes 
Request: Send a blank email to php-notes+get-58634@lists.php.net to get a copy of this message
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/","*.*"); ?> ---- Manual Page -- http://www.php.net/manual/en/function.unlink.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36583 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36583&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36583&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#58634) next »