PHP code example of ethsam / symfony-dropzone
1. Go to this page and download the library: Download ethsam/symfony-dropzone library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ethsam / symfony-dropzone example snippets
Ethsam\SymfonyDropzone\SymfonyDropzoneBundle::class => ['all' => true],
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Attachment
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private string $filename = '';
#[ORM\Column(length: 255)]
private string $src = ''; // URL or path to file
public function getId(): ?int
{
return $this->id;
}
public function getFilename(): string
{
return $this->filename;
}
public function setFilename(string $filename): self
{
$this->filename = $filename;
return $this;
}
public function getSrc(): string
{
return $this->src;
}
public function setSrc(string $src): self
{
$this->src = $src;
return $this;
}
}
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Post
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private string $title = '';
// OneToMany relationship
#[ORM\OneToMany(targetEntity: Attachment::class, mappedBy: 'post', cascade: ['persist', 'remove'])]
private Collection $attachments;
public function __construct()
{
$this->attachments = new ArrayCollection();
}
public function addAttachment(Attachment $attachment): self
{
if (!$this->attachments->contains($attachment)) {
$this->attachments->add($attachment);
}
return $this;
}
public function removeAttachment(Attachment $attachment): self
{
$this->attachments->removeElement($attachment);
return $this;
}
public function getAttachments(): Collection
{
return $this->attachments;
}
}
namespace App\Form;
use App\Entity\Attachment;
use App\Entity\Post;
use Ethsam\SymfonyDropzone\Form\DropzoneType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PostFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title', TextType::class, [
'label' => 'Post Title',
])
->add('attachments', DropzoneType::class, [
'class' => Attachment::class,
'maxFiles' => 5,
'multiple' => true,
'uploadHandler' => 'app_upload_file',
'removeHandler' => 'app_remove_file',
'acceptedFiles' => 'image/*,.pdf',
'addRemoveLinks' => true,
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Post::class,
]);
}
}
namespace App\Controller;
use App\Entity\Attachment;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
class FileController extends AbstractController
{
#[Route('/upload', name: 'app_upload_file', methods: ['POST'])]
public function upload(Request $request, EntityManagerInterface $em): JsonResponse
{
$uploadedFile = $request->files->get('file');
if (!$uploadedFile) {
return new JsonResponse(['error' => 'No file provided'], 400);
}
// Move the file to your uploads directory
$filename = uniqid() . '.' . $uploadedFile->guessExtension();
$uploadedFile->move(
$this->getParameter('kernel.project_dir') . '/public/uploads',
$filename
);
// Create and persist the attachment
$attachment = new Attachment();
$attachment->setFilename($uploadedFile->getClientOriginalName());
$attachment->setSrc('/uploads/' . $filename);
$em->persist($attachment);
$em->flush();
return new JsonResponse(['id' => $attachment->getId()]);
}
#[Route('/remove/{id}', name: 'app_remove_file', methods: ['DELETE'])]
public function remove(Attachment $attachment, EntityManagerInterface $em): JsonResponse
{
$id = $attachment->getId();
// Optionally delete the file from disk
// unlink($this->getParameter('kernel.project_dir') . '/public' . $attachment->getSrc());
$em->remove($attachment);
$em->flush();
return new JsonResponse(['id' => $id]);
}
}
#[ORM\Entity]
class Attachment
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private string $filename = '';
#[ORM\Column(length: 255)]
private string $src = '';
public function getId(): ?int { return $this->id; }
public function getFilename(): string { return $this->filename; }
public function setFilename(string $filename): self { $this->filename = $filename; return $this; }
public function getSrc(): string { return $this->src; }
public function setSrc(string $src): self { $this->src = $src; return $this; }
}
$builder->add('attachments', DropzoneType::class, [
'class' => Attachment::class,
'multiple' => true,
'maxFiles' => 10,
'uploadHandler' => 'app_upload_file',
'removeHandler' => 'app_remove_file',
]);
$builder->add('profileImage', DropzoneType::class, [
'class' => ProfileImage::class,
'multiple' => false, // Single file mode
'maxFiles' => 1,
'uploadHandler' => 'app_upload_image',
'removeHandler' => 'app_remove_image',
]);
$builder->add('photos', DropzoneType::class, [
'class' => Photo::class,
'acceptedFiles' => 'image/*',
'maxFiles' => 5,
'uploadHandler' => 'app_upload_photo',
'removeHandler' => 'app_remove_photo',
'thumbnailWidth' => 200,
'thumbnailHeight' => 200,
'thumbnailMethod' => 'contain',
'resizeWidth' => 1920,
'resizeHeight' => 1080,
'resizeMethod' => 'contain',
'resizeMimeType' => 'image/jpeg',
]);
$builder->add('documents', DropzoneType::class, [
'class' => Document::class,
'uploadHandler' => 'api_upload_document',
'removeHandler' => 'api_remove_document',
'headers' => [
'Authorization' => 'Bearer ' . $this->apiToken,
],
'formData' => [
'documentType' => 'invoice',
],
]);
{# In template #}
<div id="my-previews"></div>
{{ form_start(form) }}
{{ form_widget(form.documents) }}
{{ form_end(form) }}
{# In form builder #}
$builder->add('documents', DropzoneType::class, [
'class' => Document::class,
'uploadHandler' => 'app_upload_document',
'removeHandler' => 'app_remove_document',
'previewsContainer' => '#my-previews',
]);
$builder->add('uploads', DropzoneType::class, [
'class' => Upload::class,
'uploadHandler' => 'app_upload_file',
'removeHandler' => 'app_remove_file',
'formData' => [
'category' => 'documents',
'userId' => $this->currentUser->getId(),
],
]);
public function upload(Request $request, EntityManagerInterface $em): JsonResponse
{
$category = $request->request->get('category'); // 'documents'
$userId = $request->request->get('userId');
$file = $request->files->get('file');
// ... handle upload
}
// Before (v1)
use EmrDev\SymfonyDropzoneBundle\Form\DropzoneType;
// After (v2)
use Ethsam\SymfonyDropzone\Form\DropzoneType;