I'm trying to add a file upload to a form in cake 2.x :
Controller :
public function add() {
>
> if ($this->request->is('post')) {
> $this->request->data['Post']['user_id'] =
> $this->Auth->user('id'); //stores user id
> $filename = $this->request->data['Post']['imgur'];
> $handle = fopen($filename, "r");
> $data = fread($handle, filesize($filename));
>
> // $data is file data
> $pvars = array('image' => base64_encode($data), 'key' =>
> 'a2b334c7cb5353ae110aea1d7d51b91e');
> $timeout = 30;
> $curl = curl_init();
>
> curl_setopt($curl, CURLOPT_URL,
> 'http://api.imgur.com/2/upload.xml');
> curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
> curl_setopt($curl, CURLOPT_POST, 1);
> curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
> curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
>
> $xml = curl_exec($curl);
>
> curl_close ($curl);
>
> debug($xml);
> if ($this->Post->save($this->request->data)) {
> $this->Session->setFlash('Your post has been saved.');
> $this->redirect(array('controller' => 'users', 'action' =>
> 'timeline', $this->Auth->user('username')));
> } else {
> $this->Session->setFlash('Unable to add your post.');
> }
> }
> }
>
>
Form :
<form class="form-inline" action="/manager/posts/add" method="post"
> style="margin-bottom: 0px;">
> <input type="text" name="data[Post][game_name]"
> class="input" id="game_name" style="width:250px;">
> <button type="submit" class="btn btn-primary"><i class="icon-ok
> icon-white"></i></button>
> <input type="file" name="data[Post][imgur]" id="imgur" value="" />
>
> </form>
This should work wel, except cake send only the file name, not the full
path, so it's not uploaded to imgur :
*Warning* (2): fopen(picture.png) [function.fopen
<http://localhost:8888/manager/posts/function.fopen>]: failed to open stream:
No such file or directory [*APP/Controller/PostsController.php*, line *23*]
Any idea on how to make it work ? Perhaps with a temp folder ?
Thanks a lot !
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php