简体   繁体   中英

Drupal 8: file_move() in validate formular generate a “Recoverable fatal error”

Despite I folow this tutorial , my drupal 8 generate a "Recoverable fatal error"

There is the message of Drupal log: "Recoverable fatal error : Argument 1 passed to file_move() must implement interface Drupal\\file\\FileInterface, array given, called in.."

Here my code:

public function validateForm(array &$form, FormStateInterface $form_state) {

   $file = file_save_upload('file', array(
    // Validates file is really an image.
    'file_validate_is_image' => array(),
    // Validate extensions.
    'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  // If the file passed validation:
  if ($file) {
    // Move the file into the Drupal file system.
    if ($file = file_move($file, 'public://')) {
      // Save the file for use in the submit handler.
      $form_state['storage']['file'] = $file;
    }
    else {a
      form_set_error('file', t("Impossible d'enregistrer correctement le fichier."));
    }
  }
  else {
    form_set_error('file', t("Aucun fichier n'a été téléchargé."));
  }
  }

Is this a "right" problem? This is the basic code for upload a file.

I have added all dependencies. And more..

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
use Drupal\eshpubmanager\Mutuals\ESHPubManagerMutuals;
use Drupal\eshpubmanager\Calendar\ESHPubManagerCalendar;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Routing;
use Drupal\Core\Url; 
use Drupal\file\FileInterface;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\State\StateInterface;
use Drupal\file\Entity\File;
use Drupal\stream_wrapper_example\StreamWrapper\SessionWrapper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

Somebody have an idea?

According to the documentation, file_save_upload will return an array if multiple files are uploaded. In the error message it says that file_move was passed an array so you might be uploading multiple files or perhaps your file input element has the multiple attribute set.

Confirm this in your form build. If you want to have a single file upload and you have multiple flag remove it. If your goal is to upload multiple files you have to loop through the values returned by file_save_upload and perform file_move on each of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM