0

我使用我制作的这个自定义的简单脚本,不知何故它失败了,但它总是返回 true。

此类unlink将放置在数组中的文件通过它们然后在成功删除文件时递增计数器,这是我的脚本:

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

?>

基本上我需要改进如何使其万无一失,以便在取消链接删除文件时完全删除数组中的图像而不返回true,所以我不知道为什么它实际上没有正确删除文件。

有时它工作得很好。

4

2 回答 2

1

更改if($remove)if($remove && !file_exists($file))

另外,请注意有时在 Windows 上取消链接会失败:http: //ie.php.net/manual/en/function.unlink.php#100580

于 2011-02-24T22:08:19.347 回答
0

is_file()你可以在之后添加一个调用来unlink检查文件是否仍然存在

于 2011-02-24T22:07:24.667 回答