Author Topic: [SOLVED] unlink with wildcard?  (Read 2256 times)

0 Members and 1 Guest are viewing this topic.

Offline acctmanTopic starter

  • Devotee
    • View Profile
[SOLVED] unlink with wildcard?
« on: June 12, 2009, 06:07:00 PM »
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

Offline Crayon Violent

  • Global Moderator
  • 'Insane!'
  • *
    • View Profile
Re: unlink with wildcard?
« Reply #1 on: June 12, 2009, 06:14:32 PM »
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.

Offline MadTechie

  • PHPFreaks Recommended
  • Freak!
  • *
  • Gender: Male
  • I try to F1
    • View Profile
Re: unlink with wildcard?
« Reply #2 on: June 12, 2009, 06:14:39 PM »
no, but it shouldn't be too hard should

**totally untested**

Code: [Select]
<?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;   

?>
« Last Edit: June 12, 2009, 06:17:50 PM by MadTechie »
Computers are good at following instructions, but not at reading your mind.
After all, why would you insert your penis into a hole for no reason whatsoever?

I dunno about that.  A regular expression has a 0% chance of touching my penis.

the code is professionally made up but not working

Remember to Click Solved, how to ask questions - the smart way

Offline ToonMariner

  • Fanatic
  • Gender: Male
    • View Profile
Re: unlink with wildcard?
« Reply #3 on: June 12, 2009, 06:22:21 PM »
try this...
Code: [Select]
<?php
$files 
glob('/200_*.jpg');
array_walk($files,'unlink');
?>

follow me on twitter @PHPsycho

Offline thebadbad

  • Addict
  • Gender: Male
  • $me = str_replace($question, $answer, $post);
    • View Profile
Re: unlink with wildcard?
« Reply #4 on: June 12, 2009, 06:29:41 PM »
try this...
Code: [Select]
<?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.

Offline MadTechie

  • PHPFreaks Recommended
  • Freak!
  • *
  • Gender: Male
  • I try to F1
    • View Profile
Re: unlink with wildcard?
« Reply #5 on: June 12, 2009, 06:33:51 PM »
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  ;) )
Computers are good at following instructions, but not at reading your mind.
After all, why would you insert your penis into a hole for no reason whatsoever?

I dunno about that.  A regular expression has a 0% chance of touching my penis.

the code is professionally made up but not working

Remember to Click Solved, how to ask questions - the smart way

Offline Crayon Violent

  • Global Moderator
  • 'Insane!'
  • *
    • View Profile
Re: unlink with wildcard?
« Reply #6 on: June 12, 2009, 06:38:07 PM »
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

Offline thebadbad

  • Addict
  • Gender: Male
  • $me = str_replace($question, $answer, $post);
    • View Profile
Re: unlink with wildcard?
« Reply #7 on: June 12, 2009, 06:40:54 PM »
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.

Offline acctmanTopic starter

  • Devotee
    • View Profile
Re: unlink with wildcard?
« Reply #8 on: June 12, 2009, 06:42:58 PM »
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

Offline thebadbad

  • Addict
  • Gender: Male
  • $me = str_replace($question, $answer, $post);
    • View Profile
Re: unlink with wildcard?
« Reply #9 on: June 12, 2009, 06:48:38 PM »
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()?

Offline MadTechie

  • PHPFreaks Recommended
  • Freak!
  • *
  • Gender: Male
  • I try to F1
    • View Profile
Re: unlink with wildcard?
« Reply #10 on: June 12, 2009, 06:55:09 PM »
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!
Code: [Select]
<?php
$files 
glob('200_*.jpg');
array_walk($files,'myunlink');

function 
myunlink($t)
{
unlink($t);
}
?>

Computers are good at following instructions, but not at reading your mind.
After all, why would you insert your penis into a hole for no reason whatsoever?

I dunno about that.  A regular expression has a 0% chance of touching my penis.

the code is professionally made up but not working

Remember to Click Solved, how to ask questions - the smart way

Offline ToonMariner

  • Fanatic
  • Gender: Male
    • View Profile
Re: unlink with wildcard?
« Reply #11 on: June 12, 2009, 06:58:23 PM »
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 ;)
follow me on twitter @PHPsycho

Offline thebadbad

  • Addict
  • Gender: Male
  • $me = str_replace($question, $answer, $post);
    • View Profile
Re: unlink with wildcard?
« Reply #12 on: June 12, 2009, 07:01:57 PM »
Yes the function the user defined!, being the second parameter!

of course this "should" work!
Code: [Select]
<?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.

Offline Crayon Violent

  • Global Moderator
  • 'Insane!'
  • *
    • View Profile
Re: unlink with wildcard?
« Reply #13 on: June 12, 2009, 07:02:37 PM »
@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.

Offline thebadbad

  • Addict
  • Gender: Male
  • $me = str_replace($question, $answer, $post);
    • View Profile
Re: unlink with wildcard?
« Reply #14 on: June 12, 2009, 07:05:49 PM »
Great, just realized that too.

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.