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...