Skip to content

Latest commit

 

History

History
51 lines (40 loc) · 1011 Bytes

File metadata and controls

51 lines (40 loc) · 1011 Bytes

Basic example

Let assume you have a users table. Add a new field to your table. For example: photo

CREATE table users (
    id int(10) unsigned NOT NULL auto_increment,
    username varchar(20) NOT NULL,
    photo varchar(255)
);

In the table class:

<?php
declare(strict_types=1);

namespace App\Model\Table;
use Cake\ORM\Table;

class UsersTable extends Table
{
    public function initialize(array $config): void
    {
        $this->setTable('users');
        $this->setDisplayField('username');
        $this->setPrimaryKey('id');
        $this->addBehavior(\FileUploader\Model\Behavior\UploadBehavior::class, ['photo']);
    }
}
?>

In the form:

<?= $this->Form->create($user, ['type' => 'file']); ?>
    <?= $this->Form->control('username'); ?>
    <?= $this->Form->control('photo', ['type' => 'file']); ?>
<?= $this->Form->end(); ?>

And it's done.

The default configuration will upload the file to

WWW_ROOT/files/:table_alias/:year/:month/:timestamp.:extension