I am new to PHP. I succeeded in uploading single image to a form and saving it in the database. But when I modify that to upload multiple images, I get an error saying
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) array.
class SatelliteImages
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var array
*/
private $files= array();
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
*Set files
*
* @param UploadedFile[] $files
*/
public function setFiles($files)
{
$this->files = $files;
//return $this;
}
/**
* Get files
*
* @return UploadedFile[]
*/
public function getFiles()
{
return $this->files;
}
/**
* @param satelliteImage[] $images
*
* @return satelliteImage[]
*
*/
public function upload($images){
foreach ($this->getFiles() as $key => $file) {
$images[$key]->setImage(file_get_contents($file));
}
return $images;
}
}
This is the SatelliteImage entity(single image)
class satelliteImage
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var float
*
* @ORM\Column(name="latitude", type="float")
*/
private $latitude;
/**
* @var float
*
* @ORM\Column(name="longitude", type="float")
*/
private $longitude;
/**
* @var string
*
* @ORM\Column(name="image", type="blob", nullable=true))
*/
private $image;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set latitude
*
* @param float $latitude
*
* @return satelliteImage
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* @return float
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* @param float $longitude
*
* @return satelliteImage
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* @return float
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Set image
*
* @param string $image
*
* @return satelliteImage
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
$imgFile=$this->getFile();
$this->setImage(file_get_contents($imgFile));
// clean up the file property as you won't need it anymore
$this->file = null;
}
}
In my controller, how can I create an array of images and pass it to the above function?
/**
* @Route("/uploads", name="upload_images")
*
*/
public function uploadImages(Request $request)
{
$images=new Images();
$form = $this->createForm(ImageFile::class, $images);
$form->handleRequest($request);
$em=$this->getDoctrine()->getManager();
$images->upload(????);
}
ImageFile class:
class ImageFile extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('files', FileType::class, array(
'attr' => array(
'accept' => 'image/*',
'multiple' => 'multiple'
)
))
->add('save',SubmitType::class,array('label'=>'Insert Image','attr'=>array('class'=>'btn btn-primary','style'=>'margin-bottom:15px')))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => SatelliteImages::class
));
}
}