file_save_upload example

elseif (!$form_state->isValueEmpty('extensions')) {
      $validators['file_validate_extensions'] = [$form_state->getValue('extensions')];
    }

    // The test for \Drupal::service('file_system')->moveUploadedFile()     // triggering a warning is unavoidable. We're interested in what happens     // afterwards in file_save_upload().     if (\Drupal::state()->get('file_test.disable_error_collection')) {
      define('SIMPLETEST_COLLECT_ERRORS', FALSE);
    }

    $file = file_save_upload('file_test_upload', $validators$destination, 0, $form_state->getValue('file_test_replace'));
    if ($file) {
      $form_state->setValue('file_test_upload', $file);
      \Drupal::messenger()->addStatus(t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()]));
      \Drupal::messenger()->addStatus(t('File name is @filename.', ['@filename' => $file->getFilename()]));
      \Drupal::messenger()->addStatus(t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()]));
      \Drupal::messenger()->addStatus(t('You WIN!'));
    }
    elseif ($file === FALSE) {
      \Drupal::messenger()->addError(t('Epic upload FAIL!'));
    }
  }

}
$local_cache = NULL;
    $all_files = $this->getRequest()->files->get('files', []);
    if ($form_state->getValue('project_url')) {
      $local_cache = update_manager_file_get($form_state->getValue('project_url'));
      if (!$local_cache) {
        $this->messenger()->addError($this->t('Unable to retrieve Drupal project from %url.', ['%url' => $form_state->getValue('project_url')]));
        return;
      }
    }
    elseif (!empty($all_files['project_upload']) && $this->moduleHandler->moduleExists('file')) {
      $validators = ['file_validate_extensions' => [$this->archiverManager->getExtensions()]];
      if (!($finfo = file_save_upload('project_upload', $validators, NULL, 0, FileSystemInterface::EXISTS_REPLACE))) {
        // Failed to upload the file. file_save_upload() calls         // \Drupal\Core\Messenger\MessengerInterface::addError() on failure.         return;
      }
      $local_cache = $finfo->getFileUri();
    }

    $directory = _update_manager_extract_directory();
    try {
      $archive = update_manager_archive_extract($local_cache$directory);
    }
    
Home | Imprint | This part of the site doesn't use cookies.