dongquanyu5816 2017-06-12 09:37
浏览 97
已采纳

PHP unlink()错误:“目录不为空”

I have the following recursive method to delete a directory and all its sub-directories and files:

protected function _rrmdir($dir)
{
    if (is_dir($dir)) {
        $objects = scandir($dir);
        foreach ($objects as $object) {
            if ($object != '.' && $object != '..') {
                if (filetype($dir . '/' . $object) == 'dir') {
                    _rrmdir($dir . '/' . $object);
                } else {
                    unlink($dir . '/' . $object);
                }
            }
        }

        reset($objects);
        rmdir($dir);
    }
}

On occasion, the get a Warning, "Directory not empty".

The directory is actually created as a temporary holder for files. The files are downloaded from the Internet using the following snippet:

file_put_contents($filename, file_get_contents($file))

After they are downloaded (a write operation), they are then uploaded to a website (a read operation). Once done uploading, the temporary folder and its files are then deleted.

The odd thing is that when I look inside the temporary folder, there are no files there. It's as if the code tried to delete the folder while the last file was in the process of being deleted?

Any ideas what might be wrong and how to resolve it? I need this code to run on Windows and *nix, so a *nix only solution is not an option.

  • 写回答

1条回答 默认 最新

  • dongtan7998 2017-06-12 10:19
    关注

    The constant DIRECTORY_SEPARATOR might help you with Windows/Unix compatibility. For the folder not empty, try this:

    protected function _rrmdir($dir)
    {
        if (is_dir($dir)) {
            $objects = scandir($dir);
            foreach ($objects as $object) {
                if ($object != '.' && $object != '..') {
                    if (is_dir($dir . DIRECTORY_SEPARATOR . $object)) {
                        _rrmdir($dir . DIRECTORY_SEPARATOR . $object);
                    } else {
                        if( is_file($dir . DIRECTORY_SEPARATOR . $object) ) {
                            if(!unlink($dir . DIRECTORY_SEPARATOR . $object)) {
                                // code in case the file was not removed
                            }
                            // wait a bit here?
                        } else {
                            // code for debug file permission issues
                        }
                    }
                }
            }
            reset($objects);
            rmdir($dir);
        }
    }
    

    It might happen that you try to remove a file which permissions are not at php exec level.
    The is_file() method will return FALSE only if no read permissions, mind that write permissions are needed by the execution owner to delete files.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器