anatoliynyatin/far-symfony2-jquery-upload
Install the Bundle
composer require anatoliynyatin/far-symfony2-jquery-upload "~1"
(Note: The package name in the composer.json is anatoliynyatin/far-symfony2-jquery-upload, but the README uses faparicior/far-symfony2-jquery-upload. Use the correct namespace in AppKernel.php.)
Enable the Bundle
Add to app/AppKernel.php:
new \anatoliynyatin\FARSymfony2UploadBundle\FARSymfony2UploadBundle(),
Configure Routing
Add to app/config/routing.yml:
far_symfony2_upload:
resource: "@FARSymfony2UploadBundle/Controller/"
type: annotation
prefix: "/farupload"
Basic Configuration
Add to app/config/config.yml:
far_symfony2_upload:
prefix: "farupload"
temp_path: "%kernel.root_dir%/../web/tmp"
file_extensions_allowed: ["jpg", "png", "gif"]
Enable OneUpFlySystem Follow OneUpFlySystemBundle setup for filesystem adapters.
First Use Case Initialize BlueImp jQuery File Upload in your frontend:
$("#fileupload").fileupload({
url: "{{ path('farsymfony2upload_default_upload', {'id_session': 'unique_id_here'}) }}",
dataType: 'json',
// ... other options
});
Frontend Setup
id_session (e.g., UUID or model ID) to track uploads.Temporary Storage
temp_path (e.g., web/tmp) under a structured path:
{php_session}/{id_session}/{filename}.abc123/456/unique_filename.jpg.Backend Processing
getListFilesLocal($php_session, $id_session) to retrieve uploaded files.pathDest, dirnameDest, etc., in the returned array to define where files should move.syncFilesLocalRemote($files, $overwrite) to move files to the permanent filesystem (e.g., S3, local storage).deleteFilesLocal($files) to remove temp files post-success.// Example: Save files to a user's profile directory
$files = $FARUpload->getListFilesLocal($sessionId, $userId);
foreach ($files['files'] as &$file) {
$file['dirnameDest'] = "users/{$userId}/uploads";
}
$result = $FARUpload->syncFilesLocalRemote($files, false);
$FARUpload->deleteFilesLocal($result['files']);
Thumbnail Generation
thumbnail_driver (e.g., gd, imagine) and thumbnail_size in config.yml.{temp_path}/{thumbnail_directory_prefix} during syncFilesLocalRemote.Error Handling
file_extensions_allowed, max_file_size, and max_files_upload in the frontend and backend./farupload/upload endpoint:
{ "error": "File type not allowed" }
id_session (e.g., tied to an entity ID).id_session in a FileUpload table.syncFilesLocalRemote to a worker (e.g., for large files).FARUpload service to add pre-sync validation (e.g., duplicate filenames):
$FARUpload->addPreSyncValidator(function ($files) {
foreach ($files['files'] as $file) {
if (file_exists($file['pathDest'])) {
throw new \RuntimeException("File already exists");
}
}
});
Session Mismatch
php_session or id_session is incorrect.$php_session and id_session in the upload endpoint to debug mismatches.id_session generator (e.g., UUID) across frontend/backend.Filesystem Permissions
syncFilesLocalRemote fails silently if the destination directory lacks write permissions.OneUpFlySystemBundle adapters have correct permissions. For local storage:
chmod -R 775 %kernel.root_dir%/../web/tmp
Thumbnail Generation
imagine or gd is misconfigured.thumbnail_driver is installed (php -m | grep gd or imagine). Update config.yml:
far_symfony2_upload:
thumbnail_driver: "gd" # or "imagine"
Route Conflicts
/farupload prefix may clash with existing routes.prefix in both routing.yml and config.yml.Large File Handling
syncFilesLocalRemote for large files (>100MB).memory_limit, max_execution_time).File Extension Spoofing
file_extensions_allowed by renaming files (e.g., image.jpg.php).$mime = mime_content_type($file['pathOrig']);
if (!in_array($mime, ['image/jpeg', 'image/png'])) {
throw new \RuntimeException("Invalid file type");
}
Log File Paths
Add debug logs in your saveAction to inspect file paths:
$this->get('logger')->debug('Temp files:', ['files' => $files]);
Verify Filesystem Adapters Dump the configured adapters to ensure they’re correct:
$this->get('oneup_flysystem.filesystem_manager')->getFilesystem('remote_filesystem')->listContents('/');
Check BlueImp Console Enable BlueImp’s debug console to inspect upload progress:
$("#fileupload").fileupload({
// ...
debug: true
});
Test with debug:router
Verify routes are registered:
php bin/console debug:router | grep farupload
Custom File Processing
Override the FARUpload service to add pre/post-sync logic:
# app/config/services.yml
services:
app.far_upload:
class: anatoliynyatin\FARSymfony2UploadBundle\Service\FARUpload
arguments:
- "@oneup_flysystem.filesystem_manager"
- "@session"
calls:
- [addPreSyncValidator, ["@app.pre_sync_validator"]]
Dynamic Configuration
Load far_symfony2_upload settings from a database or API:
$config = $this->get('app.config_loader')->load('upload_settings');
$this->get('far_symfony2_upload_bundle.far_symfony2_upload_lib.service')->setConfig($config);
Event Listeners Dispatch events before/after file sync:
// In your controller
$this->get('event_dispatcher')->dispatch(
'far_upload.pre_sync',
new FarUploadEvent($files, $php_session, $id_session)
);
Custom Storage Drivers
Extend OneUpFlySystemBundle to support additional backends (e.g., Google Cloud Storage):
// Create a custom adapter and register it in oneup_flysystem.adapters
How can I help you explore Laravel packages today?