0
votes

I have used a component for uploading image,there is no problem in controller after add component.Here the code

class OesUsersController extends AppController {

var $helpers = array('Html', 'Form');
var $components = array('upload');

public function index() {
}

public function upload() 
{

    if (empty($this->data)) 
    {
        $this->render();
    }
    else 
    {
        $this->cleanUpFields();

        // set the upload destination folder
        $destination = realpath('../../app/webroot/img/uploads/') . '/';

        // grab the file
        $file = $this->data['Image']['filedata'];

        // upload the image using the upload component
        $result = $this->Upload->upload($file, $destination, null, array('type' => 'resizecrop', 'size' => array('400', '300'), 'output' => 'jpg'));

        if (!$result){
            $this->data['Image']['filedata'] = $this->Upload->result;
        } else {
            // display error
            $errors = $this->Upload->errors;

            // piece together errors
            if(is_array($errors)){ $errors = implode("<br />",$errors); }

            $this->Session->setFlash($errors);
            $this->redirect('/images/upload');
            exit();
        }
        if ($this->Image->save($this->data)) {
            $this->Session->setFlash('Image has been added.');
            $this->redirect('/images/index');
        } else {
            $this->Session->setFlash('Please correct errors below.');
            unlink($destination.$this->Upload->result);
        }
    }
}

The problem is image doesn't come from add.ctp

here the add.ctp code

<label for="Image">Image:</label>
<input type="file" name="data[Image][filedata]" id="ImageFiledata" />

add function code

    public function add() {
    $clint_ip=$this->request->clientIp();
    if ($this->request->is('post')) {

        $this->OesUser->create();

        pr($this->request->data);
        $this->request->data['OesUser']['user_otpkey']=String::uuid();
        $this->request->data['OesUser']['user_regdate']=date("Y-m-d H:i:s");
        $this->request->data['OesUser']['user_ip']=$clint_ip;
        $this->barcode($this->request->data['OesUser']['user_otpkey']);
        if ($this->OesUser->save($this->request->data)) {
            $this->Session->setFlash(__('The oes user has been saved'), 'flash_success');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The oes user could not be saved. Please, try again.'), 'flash_fail');
        }
    }
    $this->set('ip',$clint_ip);
}

here, database field name: image controller name :OesUsers Model name :OesUser

for full work I have taken help from this link http://labs.iamkoa.net/2007/10/23/image-upload-component-cakephp/

1
have you checked that if you submit it will go to the upload function? maybe you set the form wrongly. here is some resources you can read on. stackoverflow.com/questions/16262194/file-upload-in-cakephp-2-3iCezz

1 Answers

0
votes

How is your entire form looks like in add.ctp?

It sounds to me that you did not add

enctype="multipart/form-data"

to the form. That will cause the form not to post the file.

And also, it is recommended to use Form helper to create form. When using Form helper, specify the form type to file

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