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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

@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
Share on other sites

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
Share on other sites

  • 8 months later...

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
Share on other sites

  • 2 years later...
  • 5 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.