Image upload and display not work- Cakephp
Image upload and display not work- Cakephp
I'm trying to add upload image for every connected user
But I can't upload to databse
Error message :
Cannot convert value of type array to string
Edit : When I try update The User profile from (profile function) I get message :
Notice (8): Uninitialized string offset: 0 [COREsrcViewHelperUrlHelper.php, line 174]
But when I update the profile From the Users (edit function) works fine
UsersController.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']);
}
UsersTable.php
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;
}
Profile.ctp
Im not sure about this either
Notice (8): Uninitialized string offset: 0 [COREsrcViewHelperUrlHelper.php, line 174]
<?= $this->Html->image($user_data->image, ['alt' => 'User image','style' => 'height: 250px; width: 100%; display: block;']); ?>
echo $this->Form->create($user_data, ['type'=>'file']);
echo $this->Form->input('image', ['value' => $user_session['image'], 'type'=>'file']);
1 Answer
1
Two things
1) The way your code is written, the image upload block never gets called.
Lets assume you move the block to a place where it gets called. This worked on my setup. Note, I had to make a few changes to get it to work (like remove Route and change $this->request->getData to $this->request->data).
2) $this->request->data['image'] is an array. If you're trying to save the file name in $user_data->image you have to get $this->request->data['image']['name'].
Paste this in the proper area:
}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))
I use this little block of code to help me figure out where the data for your image was being saved.
This is why I changed your ['file'] to ['image'].
I always use this to see if my data is coming through from my form or not.
if($this->request->is('put') AND !empty($this->request->getData()) )
{
echo '<pre>';
print_r($this->request->data);
print_r($_FILES);
echo '</pre>';
die;
Update : after updating the profile for exemple when ever i change the name I get Notice (8): Uninitialized string offset: 0 [COREsrcViewHelperUrlHelper.php, line 174]
– Ishaq Moutanabi Benaissa
Jun 28 at 13:52
Can you post your full controller code again?
– Quasi635
Jun 28 at 17:09
ok i'll edit first post
– Ishaq Moutanabi Benaissa
Jun 29 at 21:37
Can you be more specific? Which line is line 174 in the controller code you have posted?
– Quasi635
8 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thank you it worked !
– Ishaq Moutanabi Benaissa
Jun 27 at 6:44