Jump to content

Archived

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

acctman

[SOLVED] unlink with wildcard?

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

try this...

<?php
$files = glob('/200_*.jpg');
array_walk($files,'unlink');
?>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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()?

Share this post


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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Just tested my code.. worked fine on WinXP (not saying much I know) no errors

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites

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

}

}

Share this post


Link to post
Share on other sites

Now it works:

 

$files = glob('some_path*');
array_walk($files, function ($file) {
unlink($file);
});

Share this post


Link to post
Share on other sites

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