Jump to content

[SOLVED] unlink with wildcard?


acctman

Recommended Posts

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

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);
}
?>

  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.

  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.

  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).

  • 8 months later...
  • 2 years later...
  • 5 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.