You are here

public function File::save in One Click Upload 7.2

Merge all chunks to single file

Parameters

string $destination final file location:

Return value

bool indicates if file was saved

Throws

FileLockException

FileOpenException

\Exception

1 method overrides File::save()
MongoFile::save in flowphp/src/Flow/Mongo/MongoFile.php
Merge all chunks to single file

File

flowphp/src/Flow/File.php, line 147

Class

File

Namespace

Flow

Code

public function save($destination) {
  $fh = fopen($destination, 'wb');
  if (!$fh) {
    throw new FileOpenException('failed to open destination file: ' . $destination);
  }
  if (!flock($fh, LOCK_EX | LOCK_NB, $blocked)) {

    // @codeCoverageIgnoreStart
    if ($blocked) {

      // Concurrent request has requested a lock.
      // File is being processed at the moment.
      // Warning: lock is not checked in windows.
      return false;
    }

    // @codeCoverageIgnoreEnd
    throw new FileLockException('failed to lock file: ' . $destination);
  }
  $totalChunks = $this->request
    ->getTotalChunks();
  try {
    $preProcessChunk = $this->config
      ->getPreprocessCallback();
    for ($i = 1; $i <= $totalChunks; $i++) {
      $file = $this
        ->getChunkPath($i);
      $chunk = fopen($file, "rb");
      if (!$chunk) {
        throw new FileOpenException('failed to open chunk: ' . $file);
      }
      if ($preProcessChunk !== null) {
        call_user_func($preProcessChunk, $chunk);
      }
      stream_copy_to_stream($chunk, $fh);
      fclose($chunk);
    }
  } catch (\Exception $e) {
    flock($fh, LOCK_UN);
    fclose($fh);
    throw $e;
  }
  if ($this->config
    ->getDeleteChunksOnSave()) {
    $this
      ->deleteChunks();
  }
  flock($fh, LOCK_UN);
  fclose($fh);
  return true;
}