acctman Posted June 12, 2009 Share Posted June 12, 2009 can unlink be used with a wildcard to delete a file? ie. unlink ("/200_*.jpg"); The files i'm deleting will always start and end with 200_ and .jpg Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/ Share on other sites More sharing options...
.josh Posted June 12, 2009 Share Posted June 12, 2009 I don't think so, but you can first use glob to grab the file names using wildcards and then loop through the array returned from it. Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854746 Share on other sites More sharing options...
MadTechie Posted June 12, 2009 Share Posted June 12, 2009 no, but it shouldn't be too hard should **totally untested** <?php function RecursiveUnlink($dir,$regEx='200_') { if(!$dh = @opendir($dir)) { return; } while (false !== ($obj = readdir($dh))) { if($obj == '.' || $obj == '..') { continue; }elseif(preg_match('/^'.$regEx.'*?\.jpg$/i', $obj)) { unlink($obj); } } closedir($dh); return; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854747 Share on other sites More sharing options...
ToonMariner Posted June 12, 2009 Share Posted June 12, 2009 try this... <?php $files = glob('/200_*.jpg'); array_walk($files,'unlink'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854752 Share on other sites More sharing options...
thebadbad Posted June 12, 2009 Share Posted June 12, 2009 try this... <?php $files = glob('/200_*.jpg'); array_walk($files,'unlink'); ?> I'm pretty sure you would use array_map() instead, as array_walk() only takes user-defined functions. Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854757 Share on other sites More sharing options...
MadTechie Posted June 12, 2009 Share Posted June 12, 2009 as array_walk() only takes user-defined functions. Where did you read that ? ToonMariner function looks sound to me (not a Glob fan.. but I am starting to be ) Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854761 Share on other sites More sharing options...
.josh Posted June 12, 2009 Share Posted June 12, 2009 you're both wrong. You can call it with array_walk, but you'll get a big fat "blah expects a parameter to be passed" error Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854765 Share on other sites More sharing options...
thebadbad Posted June 12, 2009 Share Posted June 12, 2009 Okay then. Was just my guess since it said Applies the user-defined function funcname to each element of the array array. in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854769 Share on other sites More sharing options...
acctman Posted June 12, 2009 Author Share Posted June 12, 2009 i received this error with array_walk Warning: Wrong parameter count for unlink() in /glob1.php on line 3 <?php $files = glob('200_*.jpg'); array_walk($files,'unlink'); ?> and with the glob('/200_*.jpg'); nothing happens no error and no file delete Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854770 Share on other sites More sharing options...
thebadbad Posted June 12, 2009 Share Posted June 12, 2009 You didn't see our posts? Here ya go: <?php $files = glob('200_*.jpg'); array_map('unlink', $files); ?> @Crayon Violent I'm just curious; how would you make array_walk() unlink the files without writing your own function to feed to array_walk()? Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854774 Share on other sites More sharing options...
MadTechie Posted June 12, 2009 Share Posted June 12, 2009 Okay then. Was just my guess since it said Applies the user-defined function funcname to each element of the array array. in the manual. Yes the function the user defined!, being the second parameter! of course this "should" work! <?php $files = glob('200_*.jpg'); array_walk($files,'myunlink'); function myunlink($t) { unlink($t); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854779 Share on other sites More sharing options...
ToonMariner Posted June 12, 2009 Share Posted June 12, 2009 hmm - learn something new - honestly never used array_map - and when I have used array_walk its been with my own defined function. still my 'methodology' was there Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854782 Share on other sites More sharing options...
thebadbad Posted June 12, 2009 Share Posted June 12, 2009 Yes the function the user defined!, being the second parameter! of course this "should" work! <?php $files = glob('200_*.jpg'); array_walk($files,'myunlink'); function myunlink($t) { unlink($t); } ?> Yeah okay, I just didn't read it like that. And no, your example won't work, array_walk() sends two parameters to the "user-defined" function - the array value as the first and the array key as the second. So the defined function should take two parameters. Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854784 Share on other sites More sharing options...
.josh Posted June 12, 2009 Share Posted June 12, 2009 @Crayon Violent I'm just curious; how would you make array_walk() unlink the files without writing your own function to feed to array_walk()? You can't. I was saying MT was wrong because array_walk() won't get the job done. I was saying you were wrong in that array_walk will indeed call a predefined function, but since you can't pass anything to unlink with it, it throws the warning. Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854785 Share on other sites More sharing options...
thebadbad Posted June 12, 2009 Share Posted June 12, 2009 Great, just realized that too. Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854790 Share on other sites More sharing options...
MadTechie Posted June 12, 2009 Share Posted June 12, 2009 Just tested my code.. worked fine on WinXP (not saying much I know) no errors Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854794 Share on other sites More sharing options...
acctman Posted June 12, 2009 Author Share Posted June 12, 2009 thanks Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854797 Share on other sites More sharing options...
thebadbad Posted June 12, 2009 Share Posted June 12, 2009 Just tested my code.. worked fine on WinXP (not saying much I know) no errors Made me test it too, and yeah, it works when you wrap unlink in a user function. Weird though, can't explain why it's okay with that, but fails with the built-in function. Edit: Actually, I think I got it: unlink() takes a stream resource as an optional second parameter. When array_walk() sends the array key as the second parameter, unlink() fails, because it expects a stream resource, but gets an integer or string. When your function myunlink() receives the second parameter, it simply ignores it (=not resulting in a fatal error). Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-854808 Share on other sites More sharing options...
tastro Posted February 14, 2010 Share Posted February 14, 2010 this works just fine for me on xp: $dir = "/yourdirectory/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false){ if( preg_match("/yourregex./ismU", $file))){echo "filename:".$file."\n";unlink($file);} } closedir($dh); } } Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-1012192 Share on other sites More sharing options...
matrlx Posted November 14, 2012 Share Posted November 14, 2012 Now it works: $files = glob('some_path*'); array_walk($files, function ($file) { unlink($file); }); Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-1392365 Share on other sites More sharing options...
FatOecim Posted May 4, 2013 Share Posted May 4, 2013 thankyou, very great code Dude.. Quote Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-1428209 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.