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
?
I had a similar issue because I wasn't adding the tmp_name of the upload. In the end I opted for normal PHP code:
move_uploaded_file ($this->request->data['Model']['picture']['tmp_name'], WWW_ROOT.'/img/pictures/'.$new_file_name.'.jpg');
$this->request->data['Model']['picture_path'] = '/img/pictures/'.$new_file_name.'.jpg';
$this->Model->save($this->request->data['Model']);
Did you ever resolve the above question without using a plugin and using Cake File capabilities? Would be interesting and useful to see.
I've tried many of those media/image uploaded plugin for CakePHP 2. I found the best one was CakeMedia (http://grafikart.github.com/CakePHP-Media). It works well except the fact that you need Auth component working.
It works well with tinymce too (already configure for you via the included helper.
you should take a look to the code It might help you.