Exemplo n.º 1
0
 /**
  * Static method that creates a unique filename whose name begins with
  * $prefix and ends with $suffix in the directory $directory. $directory
  * is a reference to a File Object.
  * Then, the file is locked for exclusive reading/writing.
  *
  * @author      manuel holtgrewe, grin@gmx.net
  * @throws      IOException
  * @access      public
  */
 public function createTempFile(string $prefix, string $suffix, File $directory)
 {
     // quick but efficient hack to create a unique filename ;-)
     $result = null;
     do {
         $result = new File($directory, $prefix . substr(md5(time()), 0, 8) . $suffix);
     } while (file_exists($result->getPath()->toNative()));
     self::$fs->createNewFile($result->getPath()->toNative());
     self::$fs->lock($result);
     return $result;
 }
Exemplo n.º 2
0
 /**
  * Unlocks a file and throws an IO Error if this is not possible.
  *
  * @throws Exception
  * @return void
  */
 public function unlock(File $f)
 {
     $filename = $f->getPath()->toNative();
     $fp = @fopen($filename, "w");
     $result = @flock($fp, LOCK_UN);
     fclose($fp);
     if (!$result) {
         throw new Exception("Could not unlock file '{$filename}'");
     }
 }