【发布时间】:2018-07-27 23:58:23
【问题描述】:
如果用户名输入john,用户名输入将创建一个目录,它将创建一个john
图片将上传到 webroot\img\users\john\uploaded1.jpg
users 表有目标字段。
它将在数据库中插入正确的目标路径 D:\xampp\htdocs\sample\webroot\img\users\john\uploaded1.jpg
但是没有添加upload1.jpg,下面的警告不能移动文件
警告 (2): move_uploaded_file(D:\xampp\htdocs\sample\webroot\img\users\james\uploaded1.jpg): 无法打开流: 没有这样的文件或目录 [APP/Controller\UsersController.php ,第 584 行] 警告 (2): move_uploaded_file() [https://secure.php.net/function.move-uploaded-file'>function.move-uploaded-file]: 无法移动 'D:\xampp-for-cakephp3\ tmp\php3FD6.tmp' 到 'D:\xampp\htdocs\sample\webroot\img\james\uploaded1.jpg' [APP/Controller\UsersController.php,第 584 行]
<?php
public function register()
{
$user = $this->Users->newEntity();
$value = $this->request->getData('username'); // holds the username input
if($this->request->is('post')) {
$image_name = $this->request->data['profile_pic']['name'];
$image_tmp = $this->request->data['profile_pic']['tmp_name'];
$destination = WWW_ROOT.'img'.DS.'users'.DS.$value.DS.$image_name;
//debug($destination);exit;
move_uploaded_file($image_tmp, $destination);
$this->request->data['profile_pic'] = $image_name;
$this->request->data['destination'] = $destination;
$user= $this->Users->patchEntity($user,$this->request->getData());
if($this->Users->save($user)) {
$dir = new Folder();
$path_data = $dir->create(WWW_ROOT.'img'.DS.'users'.DS.$value,true,0777);
$this->Flash->success(__('User profile successfuly updated.'));
return $this->redirect(['action' => 'login']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
?>
【问题讨论】:
-
您是否保存了一个冒名顶替者而不是真正的冒名顶替者?对不起,我不得不问。
-
你是什么意思 tmp ?
-
开个玩笑,对不起,无论如何,这个文件夹是否存在并且是否可写
img\james我不使用Cake,但它很可能是文件权限问题,或者至少是这样是我的首选。这几乎是在说failed to open stream: No such file or directoryaka 确保你没有使用 root 来创建它,实际上我今天在 wordpress 网站上做了类似的事情......哈哈
标签: php cakephp cakephp-3.0