Book Image

CakePHP 2 Application Cookbook

By : James Watts, Cake Software Foundation, Inc.
Book Image

CakePHP 2 Application Cookbook

By: James Watts, Cake Software Foundation, Inc.

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Uploading a file


Creating forms and submitting data is usually easy until you have to deal with file uploads. Luckily, the framework doesn't tread on any toes here and lets you build the process as your application demands.

In this recipe, we'll look at a file upload scenario and build a process that is clean and helps maintain the separation of concerns in our application.

Getting ready

To begin with, we'll need to create a table to track our file uploads. For this, create a table named uploads with the following SQL statement:

CREATE TABLE uploads (
  id INT(11) NOT NULL AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  size INT(4) UNSIGNED NOT NULL,
  mime VARCHAR(50) NOT NULL,
  path TEXT NOT NULL,
  created DATETIME,
  modified DATETIME,
  PRIMARY KEY(id)
);

We'll also need a model for our uploads table, so create a file named Upload.php in app/Model/ with the following content:

<?php
App::uses('AppModel', 'Model');

class Upload extends AppModel {
}

We're still missing a controller to deal...