xety/cake3-upload

Cake3 plugin to upload files.

Installs: 24 153

Dependents: 1

Suggesters: 0

Security: 0

Stars: 26

Watchers: 10

Forks: 17

Open Issues: 7

Type:cakephp-plugin

v1.2 2016-01-21 10:19 UTC

This package is auto-updated.

Last update: 2024-12-04 23:54:45 UTC


README

A Cake3 plugin to upload files.

Build Status Coverage Status Scrutinizer Latest Stable Version Total Downloads License

Requirements

  • CakePHP 3.X

Installation

Run : composer require xety/cake3-upload:1.* Or add it in your composer.json:

"require": {
	"xety/cake3-upload": "1.*"
},

Usage

In your config/bootstrap.php add :

Plugin::load('Xety/Cake3Upload');

In your model initialize():

$this->addBehavior('Xety/Cake3Upload.Upload', [
		'fields' => [
			'avatar' => [
				'path' => 'upload/avatar/:id/:md5'
			]
		]
	]
);

Set your form to accept files

	$this->Form->create($foo, ['type'=>'file']);
	// .. or ..
	$this->Form->create($foo, ['enctype' => 'multipart/form-data']);

To create an input to upload a file, just use the this rule : fieldName_file. Example :

<?= $this->Form->input('avatar_file', ['type' => 'file']) ?>

If specified in your Entity, add the suffixed field (e.g. avatar_file) to the $_accessible array:

	protected $_accessible = [
		# ..
        	'avatar_field' => true,
        	# ..
        	];

Identifiers

  • :id Id of the Entity (It can be the user Id if you are using this for the users table for example)
  • :md5 A random and unique identifier with 32 characters. i.e : bbebb3c3c5e76a46c3dca92c9395ee65
  • :y Based on the current year. i.e : 2014
  • :m Based on the current month. i.e : 09

Configuration

  • suffix

    Default : _file

    You can change the suffix _file to your own suffix :

    $this->addBehavior('Upload', [
    		'fields' => [
    			'avatar' => [
    				'path' => 'upload/avatar/:id/:md5'
    			]
    		],
    		'suffix' => '_anotherName'
    	]
    );
    
    <?= $this->Form->input('avatar_anotherName', ['type' => 'file']) ?>
  • overwrite

    Default : true

    This option allow you to define if the behavior must delete and/or overwrite the old file for the field. If the option is false, the file will be not uploaded if the old file name has the same name as the new name file. It can be useful if you want your users to upload only one image.

    $this->addBehavior('Upload', [
    		'fields' => [
    			'avatar' => [
    				'path' => 'upload/avatar/:id/:md5',
                    'overwrite' => false
    			]
    		]
    	]
    );
  • defaultFile

    Default : false

    This option allow you to defined a default file for the field. It can be useful if you have defined a default avatar for all your new user and you don't want to delete it (i.e : In your database as defaut value for avatar you have set : "../img/default_avatar.png"). Will work only if the overwrite is defined to true.

    $this->addBehavior('Upload', [
    		'fields' => [
    			'avatar' => [
    				'path' => 'upload/avatar/:id/:md5',
                    'overwrite' => true,
                    'defaultFile' => 'default_avatar.png'
    			]
    		]
    	]
    );
  • prefix

    Default : false

    This option allow you to defined a prefix for your upload path. Useful if you don't want to use the img/ directory for your upload.

    $this->addBehavior('Upload', [
    		'fields' => [
    			'avatar' => [
    				'path' => 'upload/avatar/:id/:md5',
    				'prefix' => '../'
    			]
    		]
    	]
    );
    Example :

    If you use a custom directory at the root of the webroot directory and you use the HtmlHelper to display your image, you can set a prefix like this :

    /**
     * The path will look like this :
     *       webroot/upload/avatar
     *
     * In the database, the record will look like that :
     *      ../upload/avatar/1/bbebb3c3c5e76a46c3dca92c9395ee65.png
     */
    
    $this->addBehavior('Upload', [
    		'fields' => [
    			'avatar' => [
    				'path' => 'upload/avatar/:id/:md5',
    				'prefix' => '../'
    			]
    		]
    	]
    );
    
    // In a view, with the Html Helper:
    <?= $this->Html->image($User->avatar) ?>
    // Output : <img src="/img/../upload/avatar/1/bbebb3c3c5e76a46c3dca92c9395ee65.png" alt="">

Contribute

Follow this guide to contribute