Working on Symfony form that has a file upload and got the above error when I try to save it.
The form is build from entity which has the following format:
class MemberFile
{
/**
* SymfonyComponentHttpFoundationFileUploadedFile object
*/
protected $file;
/**
* @ORMId
* @ORMColumn(name="file_id", type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $fileId;
/**
* @ORMColumn(type="string", length=255)
*/
protected $path;
// additional member variables and getters and setters goes here...
.....
public function upload()
{
if (null === $this->file)
return;
$this->getFile()->move(
$this->getUploadDir(), $this->file->getClientOriginalName()
);
$this->setSize($this->file->getSize());
$this->setFileType($this->file->guessExtension());
$this->path=$this->file->getClientOriginalName();
$this->file=null;
}
}
And the form is constructed inside the controller in such a way
$member_file=new MemberFile();
$form=$this->createFormBuilder($member_file)
->add("file")
->add("save", 'submit')
->getForm();
Right after the appropriate action is requested, I called $member_file->upload() after validation to get the above error..
How I solved it..
1. I checked if the file has been properly uploaded despite the error
-> yes it was uploaded
2. Checked if the original tmp file is still intact –
-> NO and BINGO!
Since the file has been moved, the subsequent operations on $this->file can not be done appropriately.
Solution
Just move those operations before you call move file..
public function upload()
{
if (null === $this->file)
return;
$this->setSize($this->file->getSize());
$this->setFileType($this->file->guessExtension());
$this->path=$this->file->getClientOriginalName();
$this->getFile()->move(
$this->getUploadDir(), $this->file->getClientOriginalName()
);
$this->file=null;
}