基本 CakePHP 2.0 图片上传

发布于 2024-12-25 08:38:08 字数 1154 浏览 6 评论 0原文

我正在尝试上传与用户记录相关的头像,但我不知道该怎么做,我想我应该使用 FileFolder 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无风消散 2025-01-01 08:38:08

我已经尝试过许多 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.

梦里°也失望 2025-01-01 08:38:08

我遇到了类似的问题,因为我没有添加上传的 tmp_name 。最后我选择了普通的 PHP 代码:

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']);

您是否曾经在不使用插件和使用 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文