1

我想知道如何在不使用集合字段类型的情况下创建多个文件上传。问题是:我有一门课,我需要在这门课上链接图像。我不需要这些图像中的任何描述和其他类型的信息,所以我只需在我的主要实体中创建附加字段。然后我添加

->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 ]);
        }
    }
}

作为我的实体类中的学说事件,当然我使文件归档像数组一样工作。但我的问题是 - 我不能第二次添加图像。例如,当我已经上传了一些图像并决定再上传一些图像时 - 他们实际上传(我可以在我的项目目录中看到它们),但我的页面上没有任何变化。

你能给我一些建议吗?或者也许有一些其他方法可以上传许多文件并且同时只有一个表单域?太谢谢了。

4

1 回答 1

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());
        }
    }
于 2013-03-26T07:19:59.410 回答