Sunday, 30 October 2011 03:01
Written by Abdul Aziz
There is something i need to acknowledge of using cakephp that make it harder to get things done vs doing it with php,
the understanding.
although cakephp is really simple to use & it simplify almost 95% of php coding,
it require a very strong fundemental of php, OOP & most importantly cakephp structure & how things work.
upload a file is quite easy to find resource from php site.
but getting it done in php require additional understanding. (inthis case behaviour)
this is my first time using behaviour in cakephp & its for uploading file.
this was not created by me.
This is the original path to the git location
but there is something missing from the manual which is the noob step. & i would like to write it here as my reference *as well as visitors
1. download from here.
2. Rename to uploadable.php
3. upload to app/models/behaviours/
4. add this to your model (which is having upload feature)
/*There is a field validaiton for photo which its accept & the path it will upload it*/
public $actsAs = array(
'Uploadable' => array(
'default' => array('accept' => false),
'photo' => array(
'accept' => array(
'image/jpeg' => array('jpg', 'jpeg'),
'image/gif' => array('gif'),
'image/png' => array('png')
),
'path' => '/uploaded/',
'prefix' => ''
)
)
);
5. add form to submit a file on your view.
/*
Recipe is the model name
Photo is the field name to save the image location
*/
echo $this->Form->create('Recipe', array('enctype' => 'multipart/form-data'));
echo $this->Form->input('photo', array('type' => 'file'));
echo $this->Form->end(__('Submit',true));
6. Test to upload.