You are here

protected function UploadController::handleUpload in Plupload integration 2.0.x

Same name and namespace in other branches
  1. 8 src/UploadController.php \Drupal\plupload\UploadController::handleUpload()

Handles multipart uploads.

Throws

\Drupal\plupload\UploadException

1 call to UploadController::handleUpload()
UploadController::handleUploads in src/UploadController.php
Handles Plupload uploads.

File

src/UploadController.php, line 152

Class

UploadController
Plupload upload handling route.

Namespace

Drupal\plupload

Code

protected function handleUpload() {

  /* @var $multipart_file \Symfony\Component\HttpFoundation\File\UploadedFile */
  $is_multipart = strpos($this->request->headers
    ->get('Content-Type'), 'multipart') !== FALSE;

  // If this is a multipart upload there needs to be a file on the server.
  if ($is_multipart) {
    $multipart_file = $this->request->files
      ->get('file', []);

    // TODO: Not sure if this is the best check now.
    // Originally it was:
    // if (empty($multipart_file['tmp_name']) ||
    // !is_uploaded_file($multipart_file['tmp_name'])) {.
    if (!$multipart_file
      ->getPathname() || !is_uploaded_file($multipart_file
      ->getPathname())) {
      throw new UploadException(UploadException::MOVE_ERROR);
    }
  }

  // Open temp file.
  if (!($out = fopen($this->temporaryUploadLocation . $this
    ->getFilename(), $this->request->request
    ->get('chunk', 0) ? 'ab' : 'wb'))) {
    throw new UploadException(UploadException::OUTPUT_ERROR);
  }

  // Read binary input stream.
  $input_uri = $is_multipart ? $multipart_file
    ->getRealPath() : 'php://input';
  if (!($in = fopen($input_uri, 'rb'))) {
    throw new UploadException(UploadException::INPUT_ERROR);
  }

  // Append input stream to temp file.
  while ($buff = fread($in, 4096)) {
    fwrite($out, $buff);
  }

  // Be nice and keep everything nice and clean.
  fclose($in);
  fclose($out);
  if ($is_multipart) {
    $this->fileSystem
      ->unlink($multipart_file
      ->getRealPath());
  }
}