我想知道如何在不使用集合字段类型的情况下创建多个文件上传。问题是:我有一个班级和这个班级我需要链接图像。我不需要在这些图像中有任何描述和其他类型的信息,所以我只是在我的主实体中创建其他字段。然后我添加
->add('files', 'file', array(
'label' => 'Images',
'required' => false,
'attr' => array(
'accept' => 'image/*',
'multiple' => true
)
在它的表单类中,这个:
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (!empty($this->files)) {
if (!empty($this->images)) {
$this->insertingKey = $this->images->count();
}
foreach ($this->files as $file) {
$imageName = uniqid('entity_') . '_' . date('Y-m-d_H:i') . '.' . $file->guessExtension();
if ($this->images === null) {
$this->images = new ArrayCollection();
}
$this->images->add($imageName);
}
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (empty($this->files)) {
return;
}
if ($this->insertingKey) {
foreach ($this->files as $file) {
$file->move($this->getUploadRootDir(), $this->images[ $this->insertingKey++ ]);
}
} else {
foreach ($this->files as $key => $file) {
$file->move($this->getUploadRootDir(), $this->images[ $key ]);
}
}
}
作为我的实体类中的教义事件,当然我使文件归档和数组一样。 但我的问题是 - 我不能第二次添加图像。例如,当我已经上传了一些图片并决定上传更多图片时 - 他们在物理上上传(我可以在我的项目目录中看到它们),但我的页面上没有任何变化。
你可以给我一些建议吗?或者也许还有一些其他方法可以通过上传多个文件并同时只有一个表单字段?非常感谢。答案 0 :(得分:1)
我用这种方式解决了这个问题:
这是我实体的代码
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if ($this->files[0] != null || !$this->files) {
if (!empty($this->images)) {
$this->insertingKey = count($this->images);
}
foreach ($this->files as $file) {
$imageName = uniqid('pref_') . '_' . date('Y-m-d_H:i') . '.' . $file->guessExtension();
if ($this->images === null) {
$this->images = array();
}
$this->images[] = $imageName;
}
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if ($this->files[0] == null || !$this->files) {
return;
}
if ($this->insertingKey) {
foreach ($this->files as $file) {
$file->move($this->getUploadRootDir(), $this->images[ $this->insertingKey++ ]);
}
} else {
foreach ($this->files as $key => $file) {
$file->move($this->getUploadRootDir(), $this->images[ $key ]);
}
}
}
public function getImagesWithAbsolutePath()
{
if (!empty($this->images)) {
$images = array();
foreach ($this->images as $image) {
$images[$image] = $this->getUploadRootDir() . '/' . $image;
}
return $images;
}
return null;
}
public function getImagesWithRelativePath()
{
if (!empty($this->images)) {
$images = array();
foreach ($this->images as $image) {
$images[$image] = $this->getUploadDir() . '/' . $image;
}
return $images;
}
return null;
}
public function getUploadRootDir()
{
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
public function getUploadDir()
{
return 'images/venue';
}
public function removeImage($imageName)
{
if ($this->images && in_array($imageName, $this->images)) {
$key = array_search($imageName, $this->images);
unset($this->images[$key]);
if (file_exists($this->getUploadRootDir() . '/' . $imageName)) {
unlink($this->getUploadRootDir() . '/' . $imageName);
}
$this->images = array_values($this->images);
}
}
这是来自控制器的一段代码:
if ($request->getMethod() === "POST") {
$form->bind($request);
if ($form->isValid()) {
$deleteImages = $request->request->get('delete_thumb', array());
if (!empty($deleteImages)) {
foreach ($request->request->get('delete_thumb') as $image) {
$imageName = substr($image, strrpos($image, '/') + 1);
$venue->removeImage($imageName);
}
}
$this->persist($venue, true);
if ($request->request->get('submit-and-quit') !== null) {
return $this->redirectToRoute('admin_list');
}
return array('form' => $form->createView());
}
}