0

I am calling the php unlink() function on a directory (user-images/1/p/) containing the following files:

1.jpg
1-s.jpg
1big.jpg
2.jpg
2-s.jpg
2big.jpg

The actual call itself is as follows:

unlink('user-images/1/p/1big.jpg');

Instead of just deleting 1big.jpg, it deletes all files with a 1 in them (1big.jpg, 1-s.jpg, 1.jpg). I've researched this quite a bit and can't seem to find anyone posting with a similar issue.

EDIT: below is the full script, not much there really, don't see how anything could be affected. I've never seen this before either :(

<?PHP
unlink('user-images/1/p/1.jpg');
unlink('user-images/1/p/1-s.jpg');
$uid = '1';
$fileName = '467';
$image = '/friskyfriends/user-images/1/p/1-big.jpg';
$width = 320;
$height = 320;
buildPics();
//buildPics($uid,$fileName,$image,$width,$height);
?>
6
  • 9
    Try a simple PHPfile with ONLY the unlink line in it and call it directly and see, if this still happens. If not, the problem might be somewhere else in your script! Somehow I doubt it's really related to the unlink-command itself. Commented Jan 31, 2012 at 7:26
  • 2
    That's crazy, it shouldn't happen and I've never heard of it happening before. a) Can you share the full script because it really sounds like there's something else wrong with it? b) Have you tried running a script that contains nothing except for the single unlink() call? Commented Jan 31, 2012 at 7:28
  • Just to experiment, try using an absolute path instead of a relative path. If that doesn't work, rename the file and delete and see what happens. Commented Jan 31, 2012 at 7:31
  • @James L - the path is actually absolute. when I originally asked the question I had used relatives, but tried absolute also, which is the state it's in now...same issues...sigh Commented Jan 31, 2012 at 7:51
  • @Alexandrew - lol, I didn't even know you could downvote 7 times! Neat... Commented Jan 31, 2012 at 7:53

4 Answers 4

7

I think you are looking for the GLOB function, which allows delete with wildcards.

example

foreach (glob("*.jpg") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
   unlink($filename);
}
Sign up to request clarification or add additional context in comments.

Comments

1

When running the php file with only the unlink code in it, it worked fine. I traced through the rest of my includes (took forever :( ) and found some issues in coding before. Still not sure why that affected a static unlink() call. That said, it was an issue elsewhere in the code which has been resolved. I appreciate the time and effort everyone put forth to help me troubleshoot this issue...

Comments

1

another approach and more definitive solution could be with a path

$search_text = "logfiles";
foreach(glob("/path/to/your/directory/$search_text*") as $filename)
{
    if(file_exists($filename)
    {
        echo "$filename size " . filesize($filename) . "\n";
        unlink($filename);
    }
    else
    {
         //no need to write because file already found with glob function
    }
}

Comments

0

Please add the unlink function for which image you want to delete.

unlink($image);

5 Comments

Why "please remove the unlink" ? How would he delete the file, then? Your code simply removes the functionality OP wants to use...
If you want to delete the one file means, using one line unlink function.
There's nothing like that in your answer...you should edit it with that explanation, so far it just seems you want him to remove the unlink altogether
Yes i have changed my answer.
it's completely baffling to me, I've never seen this. Goin to try a few more things, and if I figure it out, I'll be sure to post an update. Don't worry, didn't forget about it....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.