Installation:
composer require christmann/pluploadbundle:dev-Symfony2.3
Replace dev-Symfony2.3 with dev-Symfony2.2 or dev-master for older Symfony versions.
Enable the Bundle:
Add new CIM\PluploadBundle\CIMPluploadBundle() to app/AppKernel.php under registerBundles().
First Use Case:
Create a form type with the plupload field:
// src/Acme/DemoBundle/Form/DemoType.php
use CIM\PluploadBundle\Form\Type\PluploadType;
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('files', PluploadType::class, [
'label' => 'Upload Files',
'allowed_mime_types' => ['image/jpeg', 'image/png'],
'max_file_count' => 5,
]);
}
Ensure you have a route and controller to handle the form submission.
Twig Integration: Include the bundle’s assets in your base template:
{% block stylesheets %}
{{ parent() }}
{{ cim_plupload_assets() }}
{% endblock %}
Basic File Upload:
Use the plupload form type with minimal configuration:
$builder->add('files', PluploadType::class, [
'label' => 'Upload',
'max_file_size' => '10MB', // e.g., '10MB', '500KB'
]);
File constraint).Chunked Uploads: Enable chunked uploads for large files:
$builder->add('files', PluploadType::class, [
'chunk_size' => '1MB',
'max_chunks' => 10,
]);
CIM\PluploadBundle\Handler\UploadHandler) to manage chunks.Dynamic Configuration:
Pass runtime options via options:
$builder->add('files', PluploadType::class, [
'runtime_config' => [
'multipart' => true, // Enable multipart uploads
'url' => $this->generateUrl('upload_endpoint'),
],
]);
Integration with VichUploaderBundle:
Combine with VichUploaderBundle for persistent file storage:
use Vich\UploaderBundle\Form\Type\VichFileType;
$builder->add('file', VichFileType::class, [
'image_upload_destination' => 'uploads',
'asset' => 'file', // Field name in entity
'download_uri' => true,
]);
plupload for the upload UI and VichFileType for entity mapping.Custom Templates: Override the default Twig templates:
{# templates/CIMPlupload/plupload_widget.html.twig #}
{% extends 'CIMPluploadBundle::plupload_widget.html.twig' %}
{% block plupload_container %}
<div class="custom-upload-zone">
{{ parent() }}
</div>
{% endblock %}
Symfony Version Compatibility:
symfony/form, twig) manually.stof/doctrine-extensions + custom Plupload integration for modern Symfony.Chunked Upload Handling:
UploadHandler does not persist chunks to disk. Implement a custom handler:
// src/Acme/DemoBundle/Handler/CustomUploadHandler.php
use CIM\PluploadBundle\Handler\UploadHandler;
class CustomUploadHandler extends UploadHandler {
protected function handleChunk($chunk) {
$path = $this->getUniquePath();
file_put_contents($path, $chunk['data']);
return $path;
}
}
services:
acme.demo.handler.upload:
class: Acme\DemoBundle\Handler\CustomUploadHandler
tags: ['plupload.handler']
CSRF Protection:
{{ form_hidden(form._token) }}
csrf_token in JavaScript:
var token = '{{ csrf_token('plupload_upload') }}';
File Validation:
allowed_mime_types) is not enforced server-side by default.$file = $request->files->get('files');
if (!$file->isValid()) {
throw $this->createNotFoundException('Invalid file');
}
Asset Loading:
cim_plupload_assets() Twig function may fail if assets aren’t published.vendor/CIM/PluploadBundle/Resources/public to web/bundles/cimplupload.Check JavaScript Errors:
F12) to verify Plupload initialization:
console.log(plupload); // Should show Plupload object
jQuery or plupload.full.js.Server Logs:
APP_DEBUG=true) to log upload errors.php_error.log for file permission issues.Network Tab:
200 OK (not 403 Forbidden or 500).Custom Upload URL: Override the upload endpoint dynamically:
$builder->add('files', PluploadType::class, [
'runtime_config' => [
'url' => $this->generateUrl('dynamic_upload_route', ['id' => $entity->getId()]),
],
]);
Event Listeners:
Subscribe to Plupload events (e.g., FileUploadedEvent):
// src/Acme/DemoBundle/EventListener/UploadListener.php
use CIM\PluploadBundle\Event\FileUploadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UploadListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
'plupload.file_uploaded' => 'onFileUploaded',
];
}
public function onFileUploaded(FileUploadedEvent $event) {
$file = $event->getFile();
// Process file (e.g., store metadata)
}
}
Plupload Configuration: Extend Plupload’s runtime settings:
$builder->add('files', PluploadType::class, [
'runtime_config' => [
'filters' => [
{ 'title': 'Image files', 'extensions': 'jpg,gif,png' },
],
'resize' => ['width': 320, 'height': 240, 'quality': 90],
],
]);
'runtime_config' => ['multipart': false],
run_time to limit upload timeouts:
'runtime_config' => ['run_time': 300], // 5 minutes
How can I help you explore Laravel packages today?