February 09, 2014

8:09 am
In this article, we are going to discuss about How to create a file upload component in Cakephp. File upload is a most used component in each and every application. Here we are going to create our own file upload component in Cakephp.

To create a file upload in a form, we need to change the following in our .ctp file.

Step 1 : Create a form

Create a form in Cakephp by using the below code.

echo $this->Form->create('Company',array('type'=>'file'));

Step 2 : Create a file upload field

Use the below code to create file upload field in the form.

echo $this->Form->input('company_logo',array('type'=>'file'));

Changes in the .ctp file is completed. Now open the controller file and add the Resizer component.

App::import('Component', 'Resizer');

Step 3 : Initialize the resizer component

Open the Resizer component file and the below code to initialize the resizer component.

$this->Thumbcontent  = new ImageComponent();

Step 4 : Call the resizer function

After initialize the resizer component, call the resizer function, which at a time upload the file to the given folder location and resize the image also.

$this->Thumbcontent->resize($this->data['Company']['company_logo']['tmp_name'], WWW_ROOT.'img'.DS.'company'.DS.$time.'_'.$this->data['Company']['company_logo']['name'],550,550,false,70);


Here,

  1. First argument is the file tmp_name which you want to upload, 
  2. Second argument is the location where you want to keep the file, 
  3. Third and forth are the upload image resize with and height, 
  4. Sixth is the water mark true or false and 
  5. Seventh is image quality.

0 comments:

Post a Comment