douweng5420 2012-01-07 18:54
浏览 28

基本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?

  • 写回答

2条回答 默认 最新

  • dongwei4096 2012-07-06 22:21
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?