0

I have a form with a file upload field (limited to PDF format only). After the form has been submitted and a valid uploaded file is present, I rename and move the file. Then I try to display the page with the form again - at which point the Symfony\Component\HttpFoundation\File\File class constructor throws a FileNotFoundException because, for some reason, it's been handed the path to the no-longer-existing temp file.

Relevant facts:

  • The form fields (including the file upload field) are not mapped to an entity because I'm using a Wordpress-style "meta" table for the additional data associated with the entity; the controller handles creating any new objects to be persisted.
  • The new filename is successfully saved to the database; I've further verified that the error occurs with the $form->createView() call.
  • Before anyone suggests that this was due to my PHP settings, note that I purposefully tried uploading a PDF file with a size below the limit.
  • My temporary solution is to redirect to another page (there is no error when I do this), but longterm it's much more ideal to still show the form page after submitting the form.
  • I tried overwriting the UploadedFile object I got from the form with the File object returned by the ->move() method, but this didn't help.
  • I also tried creating a child class of the built-in FileType class and changing the data_class to be SplFileInfo (the parent class of Symfony's File class) b/c SplFileInfo doesn't throw an exception over an invalid path, but this had no effect either.

My interpretetation of what's happening is that, for some reason, when you create a form view Symfony instantiates a new File object for the file upload field using the old, temporary file path - thus resulting in a fatal error that absolutely shouldn't be happening (because when would you ever not be moving the uploaded file?).

Any suggestions for things to try that I haven't thought of yet would be much appreciated!


Solved

I figured out the problem by looking at the stack trace - I have a Twig extension that was calling Request::createFromGlobals(), and THAT resulted in trying to create a new UploadedFile object with the no-longer-existing temp file path. Having the extension get the already-existing Request object in its constructor should prevent this.

Community
  • 1
  • 1
willherzog
  • 61
  • 1
  • 9
  • Can you post (some of) your controller code? I suspect you're keeping the form throughout the process? Note the info [here](https://symfony.com/doc/current/controller/upload_file.html) just above the "Creating an Uploader Service" paragraph as that seems relevant. – ccKep Oct 06 '17 at 20:18
  • @ccKep That's not the issue - this is happening when first uploading a file (as well as after a filename has been persisted, in which case I do create a new File object - not that it seems to do anything). – willherzog Oct 09 '17 at 18:56
  • Posting the controller would still help I think. – ccKep Oct 09 '17 at 18:58
  • I did; I just responded to you first. – willherzog Oct 09 '17 at 19:09
  • Well, as far as I see you **are** using the un-altered form object? What does `$form->get($settingFile)->getData();` dump after `$em->flush();` ? – ccKep Oct 09 '17 at 19:16
  • The same UploadedFile object from before the file renaming/moving/persistence stuff. – willherzog Oct 11 '17 at 19:37

0 Answers0