Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

A simple CakePHP plugin to add file upload functionality when saving models.

License

Notifications You must be signed in to change notification settings

martinbean/cakephp-file-upload-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

CakePHP file upload plugin

A simple CakePHP plugin to add file upload functionality when saving models.

Installation

To install, either clone this repository or add it as a submodule to your project. Both are done in a command line client from the root of your project.

Cloning the repository

The simplest way is to clone the repository. The command for this is:

$ git clone git@github.com:martinbean/cakephp-file-upload-plugin.git app/Plugin/FileUpload

Adding as a submodule

Alternatively, you can add the plugin as a submodule to your project if it’s already version-controlled with Git. To do this, run the following commands:

$ git submodule add git@github.com:martinbean/cakephp-file-upload-plugin.git app/Plugin/FileUpload
$ git submodule init

For more information on submodules in Git, read http://git-scm.com/book/en/Git-Tools-Submodules.

Using the Behavior

To use the behavior, first enable the plugin in your app/Config/bootstrap.php file by adding the following line to the bottom:

CakePlugin::load('FileUpload');

Alternatively, you can just use the following if you have many plugins:

CakePlugin::loadAll();

Then use it in your models:

<?php
class Article extends AppModel {
    
    public $actsAs = array(
        'FileUpload.FileUpload'
    );
}

Configuration

The behavior has some options available:

  • allowedExtensions: (array) an array of valid file extensions
  • field: (string) the name of the field in the HTML form
  • required: (boolean) whether a field is needed for the model to validate
  • uploadDir: (string) path of upload directory, relative to webroot and ending with DS constant

You can modify these options when you attach the behavior to your models:

class Article extends AppModel {
    
    public $actsAs = array(
        'FileUpload.FileUpload' => array(
            'allowedExtensions' => array('gif', 'jpeg', 'jpg', 'png'),
            'field' => 'image',
            'required' => true,
            'uploadDir' => 'files' . DS
        )
    );
}

You also need to define a method called generateFilename() in your model that accepts one parameter. This will be the model data to be saved, so you can generate a filename from one of your model‘s other fields, or just the file’s original name.

An example that saves the file as originally named:

class Article extends AppModel {
    
    public $actsAs = array(
        'FileUpload.FileUpload'
    );
    
    public function generateFilename($data) {
        return $data['image']['name'];
    }
}

Or if you wanted to name your image upload after another field, this is how you could do it:

class Article extends AppModel {
    
    public $actsAs = array(
        'FileUpload.FileUpload'
    );
    
    public function generateFilename($data) {
        $slug = Inflector::slug(strtolower($data['title']), '-');
        $file = new File($data['name']);
        
        return sprintf('%s.%s', $slug, $file->ext());
    }
}

If you have any issues with this plugin then please feel free to create a new Issue on the GitHub repository. This plugin is licensed under the MIT License.

About

A simple CakePHP plugin to add file upload functionality when saving models.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages