FileWriteException example

if (!@copy($source$destination)) {
      // If the copy failed and realpaths exist, retry the operation using them       // instead.       $real_source = $this->realpath($source) ?: $source;
      $real_destination = $this->realpath($destination) ?: $destination;
      if ($real_source === FALSE || $real_destination === FALSE || !@copy($real_source$real_destination)) {
        $this->logger->error("The specified file '%source' could not be copied to '%destination'.", [
          '%source' => $source,
          '%destination' => $destination,
        ]);
        throw new FileWriteException("The specified file '$source' could not be copied to '$destination'.");
      }
    }

    // Set the permissions on the new file.     $this->chmod($destination);

    return $destination;
  }

  /** * {@inheritdoc} */
    $validators['file_validate_name_length'] = [];

    // Call the validation functions specified by this function's caller.     $errors = file_validate($file$validators);
    if (!empty($errors)) {
      throw new FileValidationException('File validation failed', $filename$errors);
    }

    $file->setFileUri($destinationFilename);

    if (!$this->moveUploadedFile($uploadedFile$file->getFileUri())) {
      throw new FileWriteException('File upload error. Could not move uploaded file.');
    }

    // Update the filename with any changes as a result of security or renaming     // due to an existing file.     $file->setFilename($this->fileSystem->basename($file->getFileUri()));

    if ($replace === FileSystemInterface::EXISTS_REPLACE) {
      $existingFile = $this->loadByUri($file->getFileUri());
      if ($existingFile) {
        $file->fid = $existingFile->id();
        $file->setOriginalId($existingFile->id());
      }

  protected function createMediaFromValue(MediaTypeInterface $media_type, EntityStorageInterface $media_storage$source_field_name$file) {
    if (!($file instanceof FileInterface)) {
      throw new \InvalidArgumentException('Cannot create a media item without a file entity.');
    }

    // Create a file item to get the upload location.     $item = $this->createFileItem($media_type);
    $upload_location = $item->getUploadLocation();
    if (!$this->fileSystem->prepareDirectory($upload_location, FileSystemInterface::CREATE_DIRECTORY)) {
      throw new FileWriteException("The destination directory '$upload_location' is not writable");
    }
    $file = $this->fileRepository->move($file$upload_location);
    if (!$file) {
      throw new \RuntimeException("Unable to move file to '$upload_location'");
    }

    return parent::createMediaFromValue($media_type$media_storage$source_field_name$file);
  }

  /** * Create a file field item. * * @param \Drupal\media\MediaTypeInterface $media_type * The media type of the media item. * * @return \Drupal\file\Plugin\Field\FieldType\FileItem * A created file item. */
Home | Imprint | This part of the site doesn't use cookies.