carloschininin/attach-file-bundle
Installation Add the package via Composer:
composer require carloschininin/attach-file-bundle
Publish the configuration (if needed):
php artisan vendor:publish --provider="CarlosChininin\AttachFileBundle\AttachFileServiceProvider"
Basic Usage
Register the service provider in config/app.php:
'providers' => [
// ...
CarlosChininin\AttachFileBundle\AttachFileServiceProvider::class,
],
First Use Case: File Upload
Use the AttachFile facade to handle uploads:
use CarlosChininin\AttachFileBundle\Facades\AttachFile;
$file = AttachFile::upload('file_input_name', 'path/to/storage');
Handling File Uploads
$file = AttachFile::upload('file_field', 'uploads');
// Returns an array with metadata (path, filename, etc.)
$files = AttachFile::uploadMultiple('files_field', 'uploads');
// Returns a collection of file metadata
Integration with Forms
public function store(Request $request) {
$file = AttachFile::upload($request->file('document'), 'documents');
// Process file metadata...
}
Validation and Sanitization
$file = AttachFile::upload('file_field', 'uploads', [
'max_size' => '2mb',
'allowed_types' => ['pdf', 'docx'],
]);
Storing Metadata
$user->files()->create([
'path' => $file['path'],
'original_name' => $file['original_name'],
]);
Storage Configuration
filesystems in config/filesystems.php is properly set up (e.g., local, s3).config/attach-file.php (if published).File Naming Collisions
$file = AttachFile::upload('file_field', 'uploads', [
'naming_strategy' => function ($originalName) {
return Str::uuid() . '.' . pathinfo($originalName, PATHINFO_EXTENSION);
},
]);
Missing Facade
AttachFile facade isn’t autoloaded, manually register it in AppServiceProvider:
Facades\AttachFile::class => CarlosChininin\AttachFileBundle\Facades\AttachFile::class,
dd($request->file('field_name')->isValid());
$file = AttachFile::upload('file_field', 'uploads');
\Log::info('Uploaded file:', $file);
Custom Storage Logic Override the default storage handler by binding a custom service:
$this->app->bind('attach-file.storage', function () {
return new CustomStorageHandler();
});
Event Listeners Listen for file upload events (if the package emits them):
// Example (hypothetical event)
event(new FileUploaded($file));
Middleware for Authenticated Uploads Restrict uploads to authenticated users:
Route::middleware(['auth'])->group(function () {
// Upload routes here
});
public disk. Override in config/attach-file.php:
'disk' => 's3',
chmod -R 775 storage/app/uploads
How can I help you explore Laravel packages today?