Making a temporary dir for unpacking a zipfile into

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

    Another possibility is to use the temporal file as a kind of semaphore to guarantee the unicity of the directory name. Then, create a directory whose name is based on the file name.

    define ('TMP_DIR', '/tmp'); // sys_get_temp_dir() PHP 5 >= 5.2.1
    define ('TMP_DIR_PREFIX', 'tmpdir_');
    define ('TMP_DIR_SUFFIX', '.d');
    
    /* ************************************************************************** */
    
    function createTmpDir() {
      $tmpFile = tempnam(TMP_DIR, TMP_DIR_PREFIX);
      $tmpDir = $tmpFile.TMP_DIR_SUFFIX;
      mkdir($tmpDir);
      return $tmpDir;
    }
    
    function rmTmpDir($tmpDir) {
      $offsetSuffix = -1 * strlen(TMP_DIR_SUFFIX);
      assert(strcmp(substr($tmpDir, $offsetSuffix), TMP_DIR_SUFFIX) === 0);
      $tmpFile = substr($tmpDir, 0, $offsetSuffix);
    
      // Removes non-empty directory
      $command = "rm -rf $tmpDir/";
      exec($command);
      // rmdir($tmpDir);
    
      unlink($tmpFile);
    }
    
    /* ************************************************************************** */
    

提交回复
热议问题