2

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.

1
  • 1
    I don't think you want that ['News'] level in the data. Take a look at the contents of $this->request->data and match fields that you create to that format. Commented Dec 18, 2015 at 7:21

2 Answers 2

1

There has only one problem,

$this->request->data['News']['newsImage']=$fNAME;

To

$this->request->data['newsImage']=$fNAME;

In cakephp 3 you don't need define model name. You can use browser inspector element to see field name, then match between cakephp-2 and cakephp 3 form then you can see the changed.

Sign up to request clarification or add additional context in comments.

Comments

0

$this->request->data['newsImage']=$fNAME;

After this add this line

$news=$this->News->newEntity();

After add this code ur controller looks like:

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['newsImage']=$fNAME;

   $news=$this->News->newEntity();
   $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.'));
   } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.