所以我一直在嘗試上傳多個文件並存儲它們的位置。爲了設置新文件名,我嘗試使用guessExtension()方法,如symfony文檔http://symfony.com/doc/current/controller/upload_file.html中所述,由於某些原因,此方法以及move()方法未被識別,如圖所示。這是怎麼回事?我試過包括Symfony的文件和UploadedFile類,但沒有解決問題。Symfony 3方法'guessExtension()'和'move()'找不到
AutoController.php
if($form->isValid() && $form->isSubmitted())
{
$powerType = $form['power_type']->getData();
if($powerType == 'kw')
{
$auto->setPowerKw($form['powerKw']->getData());
$auto->setPowerHp(ceil($form['powerKw']->getData() * 1.34));
}
else if($powerType == 'ag')
{
$auto->setPowerKw(ceil($form['powerKw']->getData() * 0.746));
$auto->setPowerHp($form['powerKw']->getData());
}
// PLACEHOLDER
$auto->setActive(true);
// PLACEHOLDER
// File uploading
$files = $auto->getImages();
$images = array();
foreach($files as $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move(
$this->getParameter('carAdds_directory'),
$fileName
);
$images[$fileName] = $fileName;
}
$auto->setImages($images);
$em = $this->getDoctrine()->getManager();
$em->persist($auto);
$em->flush();
return $this->render('@Auto/auto/index.html.twig');
}
實體類
/**
* @var array
*
* @Assert\Image(
* mimeTypes = {
* "image/png",
* "image/pjpeg",
* "image/jpeg",
* "image/gif"
* },
* mimeTypesMessage="Failas yra netinkamo formato",
* maxSize = "10M",
* maxSizeMessage="Failas yra per didelis"
*)
* @ORM\Column(name="images", type="array")
*/
private $images; /**
* Set images
*
* @param array $images
*
* @return Auto
*/
public function setImages($images)
{
$this->images = $images;
return $this;
}
/**
* Get images
*
* @return array
*/
public function getImages()
{
return $this->images;
}
FormType類
$builder->add('images', FileType::class, array(
'label' => false,
'label_attr' => array('class' => $label_offset),
'required' => false,
'attr' => array(
'class' => $styles,
'multiple' => 'multiple')))...
*編輯 好吧,它確實看起來這只是PHP的風暴不能識別方法因爲我沒有編譯錯誤,但是圖像不存在上傳。 Here's what my database field look like after form submission is complete.
config.yml
carAdds_directory: '%kernel.root_dir%/web/uploads/carAdds'
腓風暴說找不到報關行去。這可能是爲什麼我的文件似乎沒有上傳的問題,如果是的話,我該如何正確指定路徑?
原來這只是自動完成,但是圖片似乎並不表單提交後,在任何地方上傳。 –