2017 © Pedro Peláez
 

cakephp-plugin cake3-upload

Cake3 plugin to upload files.

image

xety/cake3-upload

Cake3 plugin to upload files.

  • Thursday, January 21, 2016
  • by Xety
  • Repository
  • 12 Watchers
  • 27 Stars
  • 16,340 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 20 Forks
  • 8 Open issues
  • 5 Versions
  • 2 % Grown

The README.md

Cake3 Upload

A Cake3 plugin to upload files., (*1)

Build Status Coverage Status Scrutinizer Latest Stable Version Total Downloads License, (*2)

Requirements

  • CakePHP 3.X

Installation

Run : composer require xety/cake3-upload:1.* Or add it in your composer.json: ``` php "require": { "xety/cake3-upload": "1.*" },, (*3)


## Usage In your `config/bootstrap.php` add : ``` php Plugin::load('Xety/Cake3Upload');

In your model initialize(): ``` php $this->addBehavior('Xety/Cake3Upload.Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5' ] ] ] );, (*4)

Set your form to accept files
``` php
    $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 : ``` php = $this->Form->input('avatar_file', ['type' => 'file']) ?>, (*5)

If specified in your Entity, add the suffixed field (e.g. `avatar_file`) to the `$_accessible` array:
``` php
    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, (*6)

    You can change the suffix _file to your own suffix : ``` php $this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5' ] ], 'suffix' => '_anotherName' ] );, (*7)

    = $this->Form->input('avatar_anotherName', ['type' => 'file']) ?> ```, (*8)

  • overwrite

    Default : true, (*9)

    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. php $this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5', 'overwrite' => false ] ] ] );, (*10)

  • defaultFile

    Default : false, (*11)

    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. php $this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5', 'overwrite' => true, 'defaultFile' => 'default_avatar.png' ] ] ] );, (*12)

  • prefix

    Default : false, (*13)

    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. ``` php $this->addBehavior('Upload', [ 'fields' => [ 'avatar' => [ 'path' => 'upload/avatar/:id/:md5', 'prefix' => '../' ] ] ] );, (*14)

    ##### 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 :
    ``` php
    /**
     * 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, (*15)

The Versions

21/01 2016

v1.x-dev

1.9999999.9999999.9999999-dev https://github.com/Xety/Cake3-Upload

Cake3 plugin to upload files.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm plugin file cakephp upload cake3

21/01 2016

dev-master

9999999-dev https://github.com/Xety/Cake3-Upload

Cake3 plugin to upload files.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm plugin file cakephp upload cake3

21/01 2016

v1.2

1.2.0.0 https://github.com/Xety/Cake3-Upload

Cake3 plugin to upload files.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm plugin file cakephp upload cake3

14/09 2015

v1.1

1.1.0.0 https://github.com/Xety/Cake3-Upload

Cake3 plugin to upload files.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin file cakephp upload cake3

02/11 2014

v1.0

1.0.0.0 https://github.com/Xety/Cake3-Upload

Cake3 plugin to upload files.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin file cakephp upload cake3