PHP code example of anomaly / file-field-type

1. Go to this page and download the library: Download anomaly/file-field-type 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/ */

    

anomaly / file-field-type example snippets


protected $fields = [
    'attachment' => [
        'type' => 'anomaly.field_type.file'
    ]
];

'document' => [
    'type'   => 'anomaly.field_type.file',
    'config' => [
        'folders' => ['documents']
    ]
]

'avatar' => [
    'type'   => 'anomaly.field_type.file',
    'config' => [
        'folders'    => ['avatars'],
        'extensions' => ['jpg', 'jpeg', 'png']
    ]
]

'image' => [
    'type'   => 'anomaly.field_type.file',
    'config' => [
        'mode'    => 'compact',
        'folders' => ['images']
    ]
]

$stream->create([
    'attachment' => 1 // File ID
]);

protected $fields = [
    'document' => [
        'type'  => 'anomaly.field_type.file',
        'rules' => [
            '

$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();
    }
}

$form = $builder->make('example.module.test');
$form->on('saving', function(FormBuilder $builder) {
    $entry = $builder->getFormEntry();
    
    // Set file ID
    $entry->attachment = 5;
});

// 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();

'attachment' => [
    'type'  => 'anomaly.field_type.file',
    'rules' => [
        '

'document' => [
    'type'  => 'anomaly.field_type.file',
    'rules' => [
        '

'avatar' => [
    'type'   => 'anomaly.field_type.file',
    'config' => [
        'folders'    => ['avatars'],
        'extensions' => ['jpg', 'jpeg', 'png', 'gif'],
        'mode'       => 'compact'
    ]
]

'contract' => [
    'type'   => 'anomaly.field_type.file',
    'config' => [
        'folders'    => ['contracts'],
        'extensions' => ['pdf']
    ],
    'rules' => [
        '

'featured_image' => [
    'type'   => 'anomaly.field_type.file',
    'config' => [
        'folders'    => ['products'],
        'extensions' => ['jpg', 'jpeg', 'png']
    ],
    'rules' => [
        '

'report' => [
    'type'   => 'anomaly.field_type.file',
    'config' => [
        'folders'    => ['reports'],
        'extensions' => ['pdf', 'doc', 'docx', 'xls', 'xlsx']
    ]
]