سؤال

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);
?>
هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

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

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

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

unlink($image);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top