title: symfony 4 error when uploading file
body: <p>I am trying to upload a file, very much following the instructions on <a href="https://symfony.com/doc/current/controller/upload_file.html" rel="nofollow noreferrer">Symfony's cookbook</a>, but it doesn't seem to work.</p>
  
  <p>The specific error is as follows, but the background reason is that the file as such does not seem to be ( or remain ) uploaded.</p>
  
  <blockquote>
    <p>Call to a member function guessExtension() on string</p>
  </blockquote>
  
  <p>As it happens, the file is momentarily created at upload_tmp_dir, but gets deleted almost immediately ( I know that 'cause I kept that directory visible on my Finder). </p>
  
  <p>The file metadata is available on the var_dump($_FILES) command on the script below.  </p>
  
  <p>So, for some reason the file is being discarded which, I believe, causes the specific error seen above.</p>
  
  <p>I believe $file ( from UploadedFile ), should receive the file as such, not the path to it, but not sure how to get there. Particularly is the file does not remain on upload_tmp_dir.</p>
  
  <p>For information, I tried the upload in a plain PHP project I have and it works fine. The file remains in upload_tmp_dir till is moved elsewhere. </p>
  
  <p>Thanks</p>
  
  <p>Here is the controller:</p>
  
  <pre><code>class ApiUserXtraController extends Controller
  {
  public function UserXtraAction(Request $request, ValidatorInterface $validator) {
  
      $is_logged = $this-&gt;isGranted('IS_AUTHENTICATED_FULLY');
      if ($is_logged) {
          $user = $this-&gt;getUser();
      }
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $repo = $em-&gt;getRepository(UserXtra::class);
  
      $userxtra = new UserXtra();
  
      $form = $this-&gt;createFormBuilder($userxtra)
          -&gt;add('imgFile', FileType::class, array('label' =&gt; 'file'))
          -&gt;add('save', SubmitType::class, array('label' =&gt; 'Create Task'))
          -&gt;getForm();
  
      var_dump($_FILES); // outputs file metadata, ie, name, type, tmp_name, size
  
      $form-&gt;handleRequest($request);
      $userxtra-&gt;setUser($user);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) { 
          /** 
           * @var UploadedFile $file 
           * */
          $file = $userxtra-&gt;getImgFile();
          var_dump('file', $file);// outputs full path to upload_tmp_dir
  
          $fileName = $this-&gt;generateUniqueFileName().'.'.$file-&gt;guessExtension(); // **THIS THROWS THE ERROR**
  
          $file-&gt;move(
              $this-&gt;getParameter('user_image_directory'),
              $fileName
          );
  
          $userxtra-&gt;setImgFile($fileName);
          //$data = json_decode($data);
          return new JsonResponse(array(
              'status' =&gt; 'ok', 
              'is_logged' =&gt; $is_logged,
          ));
      }
  
      return $this-&gt;render('upload.html.twig', array(
          'form' =&gt; $form-&gt;createView(),
      ));
  
  }
  </code></pre>