Making a temporary dir for unpacking a zipfile into

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

    Another option if running on linux with mktemp and access to the exec function is the following:

    <?php
    
    function tempdir($dir=NULL,$prefix=NULL) {
      $template = "{$prefix}XXXXXX";
      if (($dir) && (is_dir($dir))) { $tmpdir = "--tmpdir=$dir"; }
      else { $tmpdir = '--tmpdir=' . sys_get_temp_dir(); }
      return exec("mktemp -d $tmpdir $template");
    }
    
    /*example*/
    
    $dir = tempdir();
    echo "$dir\n";
    rmdir($dir);
    
    $dir = tempdir('/tmp/foo', 'bar');
    echo "$dir\n";
    rmdir($dir);
    
    // returns:
    //   /tmp/BN4Wcd
    //   /tmp/foo/baruLWFsN (if /tmp/foo exists, /tmp/baruLWFsN otherwise)
    
    ?>
    

    This avoids the potential (although unlikely) race issue above and has the same behavior as the tempnam function.

    0 讨论(0)
提交回复
热议问题