I'm trying tu upload an avatar related to an user record but I'm not sure how to do it, I think I should use File
and Folder
Utility
but I'm not sure on how I should use it.
I have an avatar
field in my users
db table where I should insert the path of my users's avatar, because the avatar is unique to every user I thought to use the avatar
column right inside the users
table, is it the right way or should I do something specific for images?
I've wrote this but it won't work:
// in my user edit view where I upload the image (avatar)
$file = new File ($this->request->data['User']['avatar']); // error here
$ext = $file->ext();
$filename = $this->User->id.$ext;
$image = $file->read();
$file->close();
$file = new File (WWW_ROOT.'img/avatar/'.$filename, true, 777);
$file->write($image);
$file->close();
$this->request->data['User']['avatar'] = $filename;
this code returns the error at the first row I've reported, where I create the File
by passing $this->request->data['User']['avatar']
array:
Warning (2): dirname() expects parameter 1 to be string, array given
Warning (2): is_dir() expects parameter 1 to be string, array given
Warning (2): basename() expects parameter 1 to be string, array given
Does exists some working example for CakePHP 2.0
?