Image Upload in Cakephp


I want to upload image from user form. But my code is not working. Please help me

My view code is here:

echo $this->Form->create('User',array('enctype'=>'multipart/form-data'));
echo $this->Form->input('cat_image',array('type'=>'file'));
echo $this->Form->end('Submit');

My Controller code is here:

move_uploaded_file($_FILES['tmp_name'], WWW_ROOT . 'img/uploads/' . $_FILES['cat_image']);

Best Solution

You have to add a 'type' tag to your form:

<?php echo $this->Form->create('User', array('type' => 'file'));?>

Then in the Controller do something like this:

if (is_uploaded_file($data['User']['image']['tmp_name']))
        '/path/to/your/image/dir/' . $this->request->data['User']['image']['name']

    // store the filename in the array to be saved to the db
    $this->request->data['User']['image'] = $this->request->data['User']['image']['name'];

And to View like this to show the image in webpage:

<?php echo $this->Html->image('/path/to/your/image/dir/' . $listShExPainImage['User']['image']); ?>

Let me know if i can help you more.

Related Question