I am trying to uploading image from controller using PHP. Here image upload is working fine but content is not saving in database, only image path saving. If I comment on image upload code then content is add in database fine.
Here is my code that I have tried
<?= $this->Form->create($news,array('type'=>'file')) ?>
<div class="col-md-12">
<?php
echo $this->Form->input('newsImage',['type'=>'file']);
echo $this->Form->input('title',['class'=>'form-control']);
echo $this->Form->input('news');
?>
</div>
<?= $this->Form->end() ?>
And in controller I have tried below code for upload image
if ($this->request->is('post')) {
$target_dir = "img/news/";
$target_file = $target_dir . basename($_FILES["newsImage"]["name"]);
$fNAME = $_FILES["newsImage"]["name"];
$TMPNAME = $_FILES['newsImage']['tmp_name'];
move_uploaded_file($_FILES["newsImage"]["tmp_name"], $target_file);
$this->request->data['News']['newsImage']=$fNAME;
$news = $this->News->patchEntity($news, $this->request->data);
if ($this->News->save($news)) {
$this->Flash->success(__('The news has been saved.'));
//return $this->redirect($this->referer());
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The news could not be saved. Please, try again.'));
}
}
Here only image directory saving in database, title and news not saving.
['News']level in the data. Take a look at the contents of$this->request->dataand match fields that you create to that format.