0

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 Answers3

0

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.

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Chris Pierce
  • 706
  • 5
  • 9
  • i did this `move_uploaded_file($image['name'], $target)` on my controller and also `Form->create(null,['url' => ['controller' => 'users', 'action' => 'update_image'], array('type'=>'file')]); ?>` to my view but still nothing happens can you instruct me more thank you sir – Mark Anthony Gersalia May 31 '17 at 03:10
  • that shouldn't have worked ... you'd need the $image['tmp_name'] to the target as that would move say /tmp/image.jdkkd.jpeg to the target .... and then you'd want to save that into the db too – Chris Pierce May 31 '17 at 03:12
  • you might consider using something like Proffer. – Chris Pierce May 31 '17 at 03:12
0

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

Bahadur Singh Deol
  • 753
  • 2
  • 6
  • 17
0

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

 <?= $this->Form->create(NULL, ['enctype' => 'multipart/form-data']) ?>
simple guy
  • 635
  • 1
  • 6
  • 15