Making a temporary dir for unpacking a zipfile into

后端 未结 7 594
心在旅途
心在旅途 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:29

    So I first found a post by Ron Korving on PHP.net, which I then modified to make a bit safer (from endless loops, invalid characters, and unwritable parent dirs) and use a bit more entropy.

    |') !== false)
        {
            return false;
        }
    
        /* Attempt to create a random directory until it works. Abort if we reach
         * $maxAttempts. Something screwy could be happening with the filesystem
         * and our loop could otherwise become endless.
         */
        $attempts = 0;
        do
        {
            $path = sprintf('%s%s%s%s', $dir, DIRECTORY_SEPARATOR, $prefix, mt_rand(100000, mt_getrandmax()));
        } while (
            !mkdir($path, $mode) &&
            $attempts++ < $maxAttempts
        );
    
        return $path;
    }
    ?>
    

    So, let's try it out:

    
    

    Result:

    /var/folders/v4/647wm24x2ysdjwx6z_f07_kw0000gp/T/tmp_900342820
    bool(true)
    bool(true)
    bool(true)
    
    /tmp/stack_1102047767
    bool(true)
    bool(true)
    bool(true)
    
    /var/folders/v4/647wm24x2ysdjwx6z_f07_kw0000gp/T/stack_638989419
    bool(true)
    bool(true)
    bool(true)
    

提交回复
热议问题