Reputation: 682
I wonder how can I create multiple file uploading without using the collection field type. The thing is: I have a class and with this class I need to link images. I don't need any descriptions and other kind of information in these images, so I just create additional field in my main entity. Then I add
->add('files', 'file', array(
'label' => 'Images',
'required' => false,
'attr' => array(
'accept' => 'image/*',
'multiple' => true
)
in it's form class and this:
/**
* @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 ]);
}
}
}
as doctrine events in my entity class, and of course I make the file filed work like and array. But my problem is - I can't add images for the second time. For instance, when I've already upload some images and decided to upload some more - they upload physically(I can see them in my project directory), but nothing's changed on my page.
Could you give me some advice guys? Or maybe there is some other methods by uploading many files and having only one form field at the same time? Great thanks.
Upvotes: 1
Views: 3099
Reputation: 682
I've solved this problem this way:
This is that code from my Entity
/**
* @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);
}
}
This is a piece of code from the controller:
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());
}
}
Upvotes: 1