anomaly / file-field_type
A file upload field type.
Installs: 53 393
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 22
Type:streams-addon
pkg:composer/anomaly/file-field_type
Requires
- anomaly/streams-platform: ^1.10
- 3.0.x-dev
- 2.3.x-dev
- v2.3.0
- 2.2.x-dev
- v2.2.38
- v2.2.37
- v2.2.36
- v2.2.35
- v2.2.34
- v2.2.33
- v2.2.32
- v2.2.31
- v2.2.30
- v2.2.29
- v2.2.19
- v2.2.18
- v2.2.17
- v2.2.16
- v2.2.15
- v2.2.14
- v2.2.13
- v2.2.12
- v2.2.11
- v2.2.10
- v2.2.9
- v2.2.8
- v2.2.7
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- 2.1.x-dev
- v2.1.14
- v2.1.13
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.19
- v2.0.18
- v2.0.17
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.1.22
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- v1.1.17
- v1.1.16
- v1.1.15
- v1.1.14
- v1.1.13
- v1.1.12
- v1.1.11
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
This package is auto-updated.
Last update: 2025-12-04 01:48:58 UTC
README
anomaly.field_type.file
A file upload field type.
The file field type provides a single file upload input with integration to the Files Module.
Features
- Single file upload
- Integration with Files Module
- BelongsTo relationship with file records
- Folder restrictions for organized uploads
- File type/extension filtering
- Drag and drop upload support
- File browsing from media library
- Preview support for images and documents
- Configurable display modes
Configuration
Basic Configuration
protected $fields = [ 'attachment' => [ 'type' => 'anomaly.field_type.file' ] ];
With Folder Restriction
'document' => [ 'type' => 'anomaly.field_type.file', 'config' => [ 'folders' => ['documents'] ] ]
With File Type Restrictions
'avatar' => [ 'type' => 'anomaly.field_type.file', 'config' => [ 'folders' => ['avatars'], 'extensions' => ['jpg', 'jpeg', 'png'] ] ]
With Display Mode
'image' => [ 'type' => 'anomaly.field_type.file', 'config' => [ 'mode' => 'compact', 'folders' => ['images'] ] ]
Usage Examples
Basic File Upload
$stream->create([ 'attachment' => 1 // File ID ]);
With File Upload in Form
protected $fields = [ 'document' => [ 'type' => 'anomaly.field_type.file', 'rules' => [ 'required' ] ] ];
Accessing Values
In Twig Templates
{# Display file name #} {{ entry.attachment.name }} {# Display file link #} <a href="{{ entry.attachment.path }}">Download {{ entry.attachment.name }}</a> {# Display image #} {% if entry.image %} <img src="{{ entry.image.path }}" alt="{{ entry.image.name }}"> {% endif %} {# Display image with manipulation #} <img src="{{ entry.avatar.make().fit(200, 200).path() }}" alt="Avatar"> {# Check if file exists #} {% if entry.document %} <p>Document attached: {{ entry.document.name }}</p> {% endif %} {# Get file properties #} {{ entry.attachment.size }} bytes {{ entry.attachment.extension }} {{ entry.attachment.mime_type }}
In PHP
$entry = $model->find(1); // Get file object $file = $entry->attachment; if ($file) { // Get file properties $name = $file->name; $path = $file->path(); $size = $file->size; $extension = $file->extension; $mimeType = $file->mime_type; // Get file ID $fileId = $entry->attachment_id; // Image manipulation if ($file->isImage()) { $thumbnail = $file->make()->fit(150, 150)->path(); } }
Setting Values
In Forms
$form = $builder->make('example.module.test'); $form->on('saving', function(FormBuilder $builder) { $entry = $builder->getFormEntry(); // Set file ID $entry->attachment = 5; });
Direct Assignment
// Set by file ID $entry->attachment_id = 10; $entry->save(); // Or set by file object $file = File::find(10); $entry->attachment()->associate($file); $entry->save();
Database Structure
The file field type stores the file relationship as:
- INTEGER - Foreign key to
files_files.id
Validation
Required File
'attachment' => [ 'type' => 'anomaly.field_type.file', 'rules' => [ 'required' ] ]
File Must Exist
'document' => [ 'type' => 'anomaly.field_type.file', 'rules' => [ 'required', 'exists:files_files,id' ] ]
Common Use Cases
User Avatar
'avatar' => [ 'type' => 'anomaly.field_type.file', 'config' => [ 'folders' => ['avatars'], 'extensions' => ['jpg', 'jpeg', 'png', 'gif'], 'mode' => 'compact' ] ]
PDF Document Upload
'contract' => [ 'type' => 'anomaly.field_type.file', 'config' => [ 'folders' => ['contracts'], 'extensions' => ['pdf'] ], 'rules' => [ 'required' ] ]
Product Featured Image
'featured_image' => [ 'type' => 'anomaly.field_type.file', 'config' => [ 'folders' => ['products'], 'extensions' => ['jpg', 'jpeg', 'png'] ], 'rules' => [ 'required' ] ]
Report Attachment
'report' => [ 'type' => 'anomaly.field_type.file', 'config' => [ 'folders' => ['reports'], 'extensions' => ['pdf', 'doc', 'docx', 'xls', 'xlsx'] ] ]
Best Practices
- Restrict Folders: Always limit uploads to specific folders for organization
- Validate Extensions: Use extension restrictions for security
- Image Optimization: Use image manipulation for thumbnails and responsive images
- Clean Up: Implement cleanup for unused file records
- Check Existence: Always check if file exists before accessing properties
- Use Relationships: Leverage Eloquent relationships for easy access
- Consider Storage: Monitor disk space for large file uploads
Requirements
- Streams Platform ^1.10
- PyroCMS 3.10+
- Files Module
License
The File Field Type is open-sourced software licensed under the MIT license.
Authors
PyroCMS, Inc. - https://pyrocms.com Ryan Thompson - support@pyrocms.com