Cakephp 2x Upload image

1.5k Views Asked by At

I'm trying to upload an image to my local folder

webroot/img/

and save the image name to database. Everything seems working fine but the image is not saving to the path

here's my view

<?php echo $this->Form->create(null,['url' => ['controller' => 'users', action' => 'update_image'],
array('enctype'=>'multipart/form-data')]); ?>
<?php echo $this->Form->input('User.id') ?>
<?php echo $this->Form->file('Profile.picture',['type'=>'file']) ?>
<?php echo $this->Form->end('submit') ?>

My controller

public function update_image(){

    $id = $this->Auth->user('id'); 
    $this->Profile->id = $id; 
    $this->set('profile', $this->User->findById($id)); 

    if ($this->request->is(array('post','put'))) {
    $frmData = $this->request->data;

        //Path to store upload image
        $target = "/teamjob_back/img/../img/".basename($frmData['Profile']['picture']);

        //Get the data from form
        $image = $frmData['Profile']['picture'];

        //save data to database
        $this->Profile->save($this->request->data);

        //Image store to the img folder
        if (move_uploaded_file($image['tmp_name']['name'], $target)) {
            echo "Image successfull";
        }
    }
}

And the code

$image['tmp_name']['name']

gives me an Illegal string offset error.

EDITED This code works on the controller

if ($this->request->is('post')) { 
        $frmData = $this->request->data;  
        $tmp = $frmData['picture']['tmp_name']; 
        $hash = rand();
        $date = date("Ymd");
        $image = $date.$hash."-".$frmData['picture']['name'];
        $target = WWW_ROOT.'img'.DS.'uploads'.DS; 

        $target = $target.basename($image); 
        if (move_uploaded_file($tmp, $target)) {
            echo "Successfully moved"; 
        }
        else
        {
            echo "Error";
        }
    }
3

There are 3 best solutions below

3
Chris Pierce On

First add

['type' => 'file']

as an option to your file instead of trying to send enctype.

Second $image is a $this->request->data['Profile']['picture'] so you can't do ['tmp_name'] and ['name'] together ... debug($this->request->data['Profile']['picture'] and you'll see that you could have

$image['tmp_name']

or

$image['name']

but not both together.

Probably more but those are good starts.

0
Bahadur Singh Deol On

First you need to remove "type => file" becuase you already use Form helper "file" function , if we are use "Form->input" then we use 'type=>file'

    e.g. $this->Form->input('email', array('type' => 'email'));
$this->Form->file('Profile.picture')

check your target path have write permission, pr($image) then you get proper array format of $image['temp_name] or $image[0]['temp_name] etc. and put your temp_name in move_uploaded_file function

e.g. move_uploaded_file(your temp_name)

Form Helper

0
simple guy On

make sure to include enctype when you create the form uploading file

 <?= $this->Form->create(NULL, ['enctype' => 'multipart/form-data']) ?>