You are on page 1of 4

AUTHOR: Nick Baker

VERSION: 1.7
EMAIL: nick@webtechnick.com

INSTALL:
copy the component file_upload.php into app/controllers/components/file_upload.php
copy the helper file_upload.php into app/views/helpers/file_upload.php

SVN:
svn checkout https://svn.xp-dev.com/svn/nurvzy_file_upload

DOWNLOAD:
http://projects.webtechnick.com/file_upload

README:
For More documentation visit the bakery @
http://bakery.cakephp.org/articles/view/file-upload-component-w-automagic-model-
optional

IN-DEPTH ARTICLE:
You can use this Component with or without a model. It defaults to use the Upload
model:

<?php
//app/models/upload.php
class Upload extends AppModel{
var $name = 'Upload';
}
?>

If you wish to NOT use a model simply set $this->FileUpload->fileModel = null; in
a beforeFilter.

<?php
//in a controller
function beforeFilter(){
parent::beforeFilter();
$this->FileUpload->fileModel = null; //Upload by default.
}
?>

If you're using a Model, you'll need to have at least 3 fields to hold the
uploaded data (name, type, size)
Example SQL Table:

--
-- Table structure for table `uploads`
--

CREATE TABLE IF NOT EXISTS `uploads` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(200) NOT NULL,
`type` varchar(200) NOT NULL,
`size` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;

Default fields are name, type, and size; but you can change that at anytime using
the $this->FileUpload->fields = array();

<?php
function beforeFilter(){
parent::beforeFilter();
//fill with associated array of name, type, size to the corresponding column
name
$this->FileUpload->fields = array('name'=> 'file_name', 'type' => 'file_type',
'size' => 'file_size');
}
?>

Depending on whether or not you are using a model your view should hold a file
input type with the name of your fileVar.

<?php
function beforeFilter(){
parent::beforeFilter();
$this->FileUpload->fileVar = 'uploads'; //file by default.
}
?>

Example View WITH Model:
<?= $form->create('Upload', array('type'=>'file')); ?>
<?= $form->input('file', array('type'=>'file')); ?>
<?= $form->end('Submit')); ?>

Example View WITHOUT a Model:
<form action="controller/action" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="Submit" />
</form>

Example using FileUploadHelper (WITH Model):
<?= $form->create('Upload', array('type'=>'file')); ?>
<?= $fileUpload->input(); ?>
<?= $form->end('Submit')); ?>

Example using FileUploadHelper (WITHOUT Model):
<?= $form->create('Upload', array('type'=>'file')); ?>
<?= $fileUpload->input(array('model' => null)); ?>
<?= $form->end('Submit')); ?>

Upon submitting a file the FileUpload Component will automatically search for your
uploaded file, verify its of the proper type set by $this->FileUpload-
>allowedTypes:
<?php
function beforeFilter(){
parent::beforeFilter();
//defaults to 'image/jpeg','image/gif','image/png','image/pjpeg','image/x-png'
$this->FileUpload->allowedTypes = array('image/jpeg','text/plain');
}
?>

Then it will attempt to copy the file to your uploads directory set by $this-
>FileUpload->upload_dir:
<?php
function beforeFilter(){
parent::beforeFilter();
//defaults to 'files', will be webroot/files, make sure webroot/files exists and
is chmod 777
$this->FileUpload->uploadDir = 'files';
}
?>

If a fileModel is given, it will attempt to save the record of the uploaded file
to the database for later use. Upon success the FileComponent sets $this-
>FileUpload->success to TRUE; You can use this variable to test in your controller
like so:

<?php
class UploadsController extends AppController {

var $name = 'Uploads';
var $helpers = array('Html', 'Form');
var $components = array('FileUpload');

function admin_add() {
if(!empty($this->data)){
if($this->FileUpload->success){
$this->set('photo', $this->FileUpload->finalFile);
}else{
$this->Session->setFlash($this->FileUpload->showErrors());
}
}
}
}
?>

To View the photo variable you might type something like
$html->image("/files/$photo");

Example with FileUploadHelper:
$fileUpload->image($photo);

At any time you can remove a file by using the $this->FileUpload-
>removeFile($name); function. An example of that being used might be in a
controller:
<?php
class UploadsController extends AppController {
var $name = 'Uploads';
var $helpers = array('Html', 'Form', 'FileUpload');
var $components = array('FileUpload');

function admin_delete($id = null) {
$upload = $this->Upload->findById($id);
if($this->FileUpload->removeFile($upload['Upload']['name'])){
if($this->Upload->del($id)){
$this->Session->setFlash('Upload deleted');
$this->redirect(array('action'=>'index'));
}
}
}
}
?>

Simple as that. Automagic File Uploading. I hope you enjoy it. If you read through
the documentation I've written in the actual FileUpload Component it will give you
detailed examples and explanations of each variable/function. Comments are
appreciated.

ADDITIONAL CONTRIBUTIONS:

@Elmer 2/9/2009 (http://bakery.cakephp.org/articles/view/file-upload-component-w-
automagic-model-optional):
"As long as the automatic var is true (default), the component works as always.
But if it is set to false, processFile() is no longer called automatically. From
my controller I can now set an upload folder and call processFile() when I'm
ready."

if ($this->FileUpload->hasFile) {
$this->FileUpload->uploadDir = 'files/sub/dir/1/2/3';
$this->FileUpload->processFile();
}