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 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. 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; } ?> 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'); ?> 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 Quote 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. 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 Quote 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 ) 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 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 Quote Applies the user-defined function funcname to each element of the array array. in the manual. 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 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()? 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 Quote Okay then. Was just my guess since it said Quote 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); } ?> 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 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 Quote 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. 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 Quote @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. 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. 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 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 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 Quote 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). 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); } } 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); }); 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.. Link to comment https://forums.phpfreaks.com/topic/161998-solved-unlink-with-wildcard/#findComment-1428209 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.