Git Product home page Git Product logo

cakephp-file-storage-behavior's Introduction

CakePHP File Storage Behavior Build Status

A file storage plugin for CakePHP 2.x. For the CakePHP 1.x behavior see the cakephp1 branch.

Handles attaching files to models and storing uploaded files in database or filesystem. If uploading to filesystem will store metadata in database.

Files saved in the filesystem will be saved in a directory hierarchy based on the hash of the file contents. Filenames are not used, so will never clash. The hierarchy is to ease performance issues when storing a very large number of files.

Installation

If you're using composer then just add the following to your require block.

	"burriko/cake-file-storage": "2.1.*@dev"

If you're not, then clone/copy the contents of this directory to app/Plugins/CakeFileStorage.

Configure

  1. Add the following line to your app/Config/bootstrap.php.

     CakePlugin::load('CakeFileStorage');
    
  2. In your model add:

     public $actsAs = array('CakeFileStorage.FileStorage');
    
  3. Your model's database schema will need fields for filename, type and size. If storing the file in the filesystem you will also need one named hash, and if storing in db one named content. Here is an example schema.

     CREATE TABLE `files` (
       `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
       `filename` varchar(100) NOT NULL,
       `type` varchar(100) NOT NULL,
       `size` mediumint(8) unsigned NOT NULL,
       `content` mediumblob NOT NULL,
       `hash` varchar(40) NOT NULL,
       `created` datetime NOT NULL,
       `modified` datetime NOT NULL,
       PRIMARY KEY (`id`)
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

Usage

When saving this model, if there is a form field named 'file' it will be saved as a file using the behavior. By default it will be saved to the filesystem in an 'uploads' folder in the root folder of your app. The defaults can be changed by passing settings to the behavior as follows.

	public $actsAs = array(
		'CakeFileStorage.FileStorage' => array(
			'storage_type' => 'filesystem',
			'file_path' => '/path/to/files'
			'field_name' => 'my_file'
		)
	);

The behaviour also provides a validation message to check that the file uploaded without any problems. This can be used as follows.

	public $validate = array(
		'file' => array(
			'rule' => 'checkFileUpload',
			'message' => 'There was a problem uploading your file.'
		)
	);

You can retrieve the file from the model by using the fetchFile() method, and send the file back as a response by using the downloadFile() method of the FileStorage component. Here's an example controller that puts those together.

	class DocumentsController extends AppController
	{
		public $components = array('CakeFileStorage.FileStorage');

		public function download($id)
		{
			$document = $this->Document->fetchFile($id);

			return $this->FileStorage->createFileResponse($document);
		}
	}

When deleting a record the file will also be removed from disk, but only if no other record is pointing to the same file (because files are saved under a hash of their contents, if the same file was uploaded for multiple records they would all share the same file on disk).

cakephp-file-storage-behavior's People

Contributors

graemetait avatar tfohlmeister avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cakephp-file-storage-behavior's Issues

Store a file without a form

Hi there,

A question more than an issue...

I successfully used the behavior to store user-uploaded files in my application.
Now I have a requirement where the file, a .PDF, instead of being uploaded is generated by the application.
Before I start digging into the code:
Is there a way to use the behavior in such a way so that the file to store is passed NOT from $_POST but directly as part of the data to store ?

Unable to debug() find-result with FileStorage dependency

In my setup here I have two models:

  • Files (which actsAs the FileStorage)
  • Events, which belongsTo Files

Whenever I do a $events = $this->Events->find('all'); and try to debug($result) the result, I get an empty output. This doesn't happen if I skip the Event['File'] property, e.g. by only selecting debug($events['Event']). I am currently using the latest DebugKit.

Any suggestion on how to fix this? This is a major issue, as it also affects JSON encoding of results (empty as well).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.