用户名输入将创建一个目录,如果用户名输入john,它将创建john
图片将上传至webroot \ img \ users \ john \ uploaded1.jpg
用户表有目标字段。
它将在数据库中插入正确的目标路径D:\ xampp \ htdocs \ sample \ webroot \ img \ users \ john \ uploaded1.jpg
但是没有添加uploaded1.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']);
}
?>
答案 0 :(得分:0)
此错误是因为该文件夹不存在,因此要克服此错误,您必须首先使用USER INPUT创建该文件夹。
考虑你的例子,用户输入是John
$value = $this->request->getData('username'); //User input
$image_name = $this->request->data['profile_pic']['name'];
$image_tmp = $this->request->data['profile_pic']['tmp_name'];
$user_input_folder = WWW_ROOT.'img'.DS.'users'.DS.$value.DS;
$destination = $user_input_folder.$image_name;
if (!file_exists($user_input_folder)) { //Checks file or folder exist or not
if(mkdir($filePath, 0777, true)){
$oldMask = umask(0);
chmod($filePath, 0777);
umask($oldMask);
//Then do your file upload
echo $destination;
var_dump(move_uploaded_file($image_tmp, $destination));die;
} else {
pr("User input contains at least one illegal character, so can't create folder.");die;
}
}