我正在尝试为每个已连接的用户添加上传图片
但是我无法上传到数据库
错误消息:
无法将类型数组的值转换为字符串
编辑:当我尝试更新(配置文件功能)的用户配置文件时,出现以下消息: 注意(8):未初始化的字符串偏移量:0 [CORE \ src \ View \ Helper \ UrlHelper.php,第174行]
但是当我更新个人资料时,来自用户(编辑功能)的效果很好
UsersController.php
self.display.stop()
File "/usr/local/lib/python2.7/dist-
packages/pyvirtualdisplay/abstractdisplay.py", line 119, in stop
self.redirect_display(False)
File "/usr/local/lib/python2.7/dist-
packages/pyvirtualdisplay/abstractdisplay.py", line 88, in
redirect_display
del os.environ['DISPLAY']
File "/usr/lib/python2.7/os.py", line 498, in __delitem__
del self.data[key]
KeyError: 'DISPLAY'
UsersTable.php
public function edit($id = null)
{
$this->viewBuilder()->setLayout('user');
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
if ($this->request->data['password'] == '') {
unset($this->request->data['password']);
}
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
unset($user->password); // Unset the password so nothing is loaded or displayed in the form
$roles = $this->roles;
$this->set(compact('user', 'roles'));
$this->set('_serialize', ['user']);
$this->set('user_session', $this->request->getSession()->read('Auth.User'));
}
public function profile()
{
//Set Title
$this->set('title', 'Profile');
// Set the layout.
$this->viewBuilder()->setLayout('user');
$this->set('user_session', $this->request->session()->read('Auth.User'));
$Users = TableRegistry::get('Users');
$user_data = $Users->get($this->request->session()-
>read('Auth.User.id'));
if($this->request->is('put') AND !empty($this->request->getData()) )
{
$userdata = $Users->patchEntity($user_data, $this->request->getData(), [
'validate' => 'update_profile'
]);
if($userdata->errors())
{
// Form Validation TRUE
$this->Flash->error('Please Fill required fields');
}else
{
$user_data->name = $this->request->getData('full_name');
$user_data->username = $this->request->getData('username');
$user_data->email = $this->request->getData('email');
$user_data->image = $this->request->data['image']['names'];
//upload image
if(!empty($this->request->data['file']['names'])){
$filename = $this->request->data['file']['names'];
$url = Router::url('/', true) . 'img/' . $filenames;
print_r($url);
exit();
$uploadpath = 'img/';
$uploadfile = $uploadpath . $filenames;
if (move_uploaded_file($this->request->data['file']['tmp_name'], $uploadfile)) {
$user = $this->Users->newEntity();
$user_data->names = $filenames;
$user_data->image = $url;
}
}
// Form Validation FALSE
if($Users->save($user_data))
{
// User Session Update
$this->request->session()->write('Auth.User.name', $user_data['full_name']);
$this->request->session()->write('Auth.User.username', $user_data['username']);
$this->request->session()->write('Auth.User.email', $user_data['email']);
$this->request->session()->write('Auth.User.image', $user_data['image']);
$this->redirect('/Users/Profile');
$this->Flash->success('User has been Updated.');
}else{
$this->Flash->error(__('Unable to update user!'));
}
}
}
$this->set(compact('user_data'));
$this->set('_serialize', ['user_data']);
}
Profile.ctp 我也不确定
通知(8):未初始化的字符串偏移量:0 [CORE \ src \ View \ Helper \ UrlHelper.php,第174行]
public function validationUpdate_profile(Validator $validator)
{
$validator
// User Funll Name Validation
->notEmpty('full_name')
// User Phone Validation
->notEmpty('email')
->allowEmpty('image')
->allowEmpty('image_dir');
return $validator;
}
答案 0 :(得分:0)
两件事
1)编写代码的方式,永远不会调用图像上传块。
让我们假设您将块移动到调用它的地方。这适用于我的设置。注意,我必须进行一些更改才能使其正常工作(例如删除Route并将$ this-> request-> getData更改为$ this-> request-> data)。
2)$ this-> request-> data ['image']是一个数组。如果要尝试将文件名保存在$ user_data-> image中,则必须获取$ this-> request-> data ['image'] ['name']。
将其粘贴到适当的区域:
}else
{
$user_data->name = $this->request->data('full_name');
$user_data->image = $this->request->data['image']['name'];
//upload image
if(!empty($this->request->data['image']['name'])){
$filename = $this->request->data['image']['name'];
$url = WWW_ROOT . 'img/' . $filename;
$uploadpath = 'img/';
$uploadfile = $uploadpath . $filename;
if (move_uploaded_file($this->request->data['image']['tmp_name'], $uploadfile)) {
$user = $this->Users->newEntity();
$user_data->name = $filename;
$user_data->image = $url;
}
}
// Form Validation FALSE
if($Users->save($user_data))
我使用这小段代码来帮助我弄清楚图像数据的保存位置。
这就是为什么我将您的['file']更改为['image']的原因。
我总是使用它来查看我的数据是否来自表单。
if($this->request->is('put') AND !empty($this->request->getData()) )
{
echo '<pre>';
print_r($this->request->data);
print_r($_FILES);
echo '</pre>';
die;