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>