How to update image to db in cakephp 3.6.7

how to fetch image link from database and image from folder in cakephp 3.6.7
please Help me,

thanks & regards,
Darshan

while updating image link into database it will store first character only and shown error

Warning (2): Illegal string offset ‘name’ [APP/Controller\AuthexsController.php, line 92]
Warning (2): Illegal string offset ‘tmp_name’ [APP/Controller\AuthexsController.php, line 96]
Warning (2): Illegal string offset ‘name’ [APP/Controller\AuthexsController.php, line 96]

code is:
if($this->request->is(‘post’))
{
$profilepic=$this->request->data[‘image’][‘name’];

 $myfolder= WWW_ROOT.DS."files".DS."uploads";
 $dir = new Folder($myfolder, true, 0755); 

 move_uploaded_file($this->request->data['image']['tmp_name'], $myfolder.DS.$this->request->data['image']['name']);
			 
			$users_table = TableRegistry::get('users');
			$users = $users_table->get($id);
			 
			$users->imagenew = $profilepic;	
			 
     
			if($users_table->save($users))
		 $this->Flash->success('User is Updated');
	   $session = $this->request->session();
			 $session->write('user_id',$users);
			return $this->redirect($this->Auth->redirectUrl());
		}

help me plz

thanks & regards,
Darshan

I assume you image located in path : project/webroot/uploads/xxxxx.jpg
And xxxxx.jpg file name saved in tables users in fileds ‘imagenew
To show the image in File: src/Template/Users/view.ctp

<?php
echo $this->Html->image('/uploads/'.$user->imagenew, ['alt' => 'Picture']);

okey…,

but how to update the photo …?

You can use like edit function and after save success, use move_uploaded_file to the uploads directory

this is my updatecontroller page

$profilepic=$this->request->data[‘image’][‘name’];
$myfolder= WWW_ROOT.DS.“files”.DS.“uploads”;
$dir = new Folder($myfolder, true, 0755);
move_uploaded_file($this->request->data[‘image’][‘tmp_name’], $myfolder.DS.$this->request->data[‘image’][‘name’]);

error
Warning (2): Illegal string offset ‘name’ [APP/Controller\AuthexsController.php, line 92]
Warning (2): Illegal string offset ‘tmp_name’ [APP/Controller\AuthexsController.php, line 95]
Warning (2): Illegal string offset ‘name’ [APP/Controller\AuthexsController.php, line 95]

in database only first character is storing and shows error…?

help me yar…

Can you share code HTML Form

html code: echo $this->Form->input(‘image’, [‘type’ => ‘file’]);

controller code:

$profilepic=$this->request->data[‘image’][‘name’];

$myfolder= WWW_ROOT.DS.“files”.DS.“uploads”;

$dir = new Folder($myfolder, true, 0755);

move_uploaded_file($this->request->data[‘image’][‘tmp_name’], $myfolder.DS.$this->request->data[‘image’][‘name’]);

$users_table = TableRegistry::get(‘users’);
$users = $users_table->get($id);

$users->imagenew = $profilepic;

if($users_table->save($users))
$this->Flash->success(‘User is Updated’);

Do you put this code in your form helper?

echo $this->Form->create($users, ['type' => 'file']);

Just want to know, you put this array in the form ['type' => 'file']

i tried its Its not working

ya bro working i forgot to add

‘enctype’ => ‘multipart/form-data’

its not working…

Yes, and Good job. It’s point I want to you find it, because value from your FROM not passing multipart/form-data.

Maybe like this put in value of existing name to new input as 'old_name’

echo $this->Form->control('old_name',['value'=>$user->name]);

In Controller, for delete the file.

$oldprofilepic=$this->request->data[‘image’][‘old_name’];
$myfolder= WWW_ROOT.DS.“files”.DS.“uploads”;
$dir = new Folder($myfolder, true, 0755);
$file = new File($myfolder.DS.$oldprofilepic);
$file->delete();
$file->close(); 

OR Just simple like unlink($myfolder.DS.$oldprofilepic);

This code just the idea. Not tested.

1 Like

that is not working…

how to get jQuery function to cakephp header…?

plz help me…

How you declare jquery lib?

i tried as script but its not working…

Basically, we put file
*.js in webroot/js
*.css in webroot/css
*.jpg in webroot/img

in src/Template/Layout/defau.ctp for example configure for bootstrap

<!DOCTYPE html>
<html>
<head>
    <!-- CSS Bootstrap 3.3.6 -->
    <?php echo $this->Html->css('bootstrap.min'); ?>
    <!-- Custom style exp: style.css-->
    <?php echo $this->Html->css('style'); ?>


    <?= $this->fetch('meta') ?>
    <?= $this->fetch('css') ?>
    <?= $this->fetch('script') ?>
</head>
<body>
    <?= $this->Flash->render() ?>
    <div class="container clearfix">
        <?= $this->fetch('content') ?>
    </div>

    <!-- jQuery 2.2.3 -->
    <?php echo $this->Html->script('jquery-2.2.3.min'); ?>
    <!-- JS Bootstrap 3.3.6 -->
    <?php echo $this->Html->script('bootstrap.min'); ?>
    <!-- Custom Js exp: tools.js-->
    <?php echo $this->Html->css('tool'); ?>
</body>
</html>