Frage

I use this custom, simple script I made, somehow it's failing but it always returns true.

This class unlink the files that are placed in an array which goes through them then increments the counter on success of a file that was deleted, this is my script:

<?php

class Delete 
{
    function deleteFiles($array)
    {
        if(is_array($array))
        {
            $filecount = count($array);
            $count = 0;

            foreach($array as $file)
            {
                if(file_exists($file))
                {
                    $remove = unlink($file);

                    if($remove)
                    {
                        $count++;   
                    }
                }
                else
                {
                    return false;
                }
            }

            if($count == $filecount)
            {
                return true;    
            }
            else
            {
                return false;   
            }
        }
        else
        {
            return false;   
        }
    }
}

?>

Basically I need improvements on how to make it foolproof so that the images in the array are completely deleted not returning true once an unlink has deleted the file when it hasn't, so I have no idea why it's not actually deleting the files properly.

Sometimes it works perfectly.

War es hilfreich?

Lösung

change if($remove) to if($remove && !file_exists($file))

Also, be aware that sometimes an unlink will fail on Windows: http://ie.php.net/manual/en/function.unlink.php#100580

Andere Tipps

you could add a call to is_file() after the unlink to check if the file is still there

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top