Making a temporary dir for unpacking a zipfile into

后端 未结 7 593
心在旅途
心在旅途 2020-12-29 23:05

I have a script that checks a zipfile containing a number of matching PDF+textfiles. I want to unpack, or somehow read the textfiles from the zipfile, and just pick out some

7条回答
  •  自闭症患者
    2020-12-29 23:30

    The "mkdir" function raises a warning if the directory already exists, so you can catch this using "@mkdir" and avoid any race condition:

    function tempDir($parent = null)
    {
        // Prechecks
        if ($parent === null) {
            $parent = sys_get_temp_dir();
        }
        $parent = rtrim($parent, '/');
        if (!is_dir($parent) || !is_writeable($parent)) {
            throw new Exception(sprintf('Parent directory is not writable: %s', $parent));
        }
    
        // Create directory
        do  { 
            $directory = $parent . '/' . mt_rand();
            $success = @mkdir($directory);
        }
        while (!$success);
    
        return $directory; 
    }
    

提交回复
热议问题