基本 CakePHP 2.0 图片上传
我正在尝试上传与用户记录相关的头像,但我不知道该怎么做,我想我应该使用 File
和 Folder
Utility
但我不确定应该如何使用它。
我的 users
数据库表中有一个 avatar
字段,我应该在其中插入用户头像的路径,因为该头像对于我认为使用 的每个用户来说都是唯一的
列,这是正确的方法还是我应该针对图像做一些特定的事情?users
表内的 >avatar
我已经写了这个,但它不起作用:
// 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->request- 创建
数组:File
>data['User']['avatar']
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
是否存在 CakePHP 2.0
的一些工作示例?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经尝试过许多 CakePHP 2 的媒体/图像上传插件。我发现最好的一个是 CakeMedia (http ://grafikart.github.com/CakePHP-Media)。它运行良好,除了您需要 Auth 组件工作这一事实。
它也适用于tinymce(已经通过包含的帮助程序为您配置。
您应该查看代码它可能会对您有所帮助。
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.
我遇到了类似的问题,因为我没有添加上传的 tmp_name 。最后我选择了普通的 PHP 代码:
您是否曾经在不使用插件和使用 Cake File 功能的情况下解决过上述问题?看看会很有趣且有用。
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:
Did you ever resolve the above question without using a plugin and using Cake File capabilities? Would be interesting and useful to see.