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";
}
}