0

Im trying to edit an upload an image to my database. However the image field do not get the value and comes out the filed is null.

view:

            <?php foreach($blog as $b){?>
                <form method="post" action="<?php echo site_url('adminUpdatePost/'.$b->id); ?>" class="col s12">     
                    <div class="row">
                        <div class="input-field col s12">
                            <textarea id="textarea1" name="textarea1" class="materialize-textarea"><?=$b->post?></textarea>
                            <label for="textarea1">Post</label>
                        </div>
                    </div>

                    <div class="file-field input-field">
                       <div class="btn">
                            <span>Post Image</span>
                            <input type="file" name="file">
                        </div>
                        <div class="file-path-wrapper">
                            <input id="image" name="image" class="file-path validate" placeholder="<?=$b->image?>" type="text">
                      </div>
                    </div>

                    <input type="submit" value="Submit">                    
                </form>
                <?php } ?>

controller:

public function updatePost($id)
{
    $config = array(
        'upload_path' => 'uploads/',
        'allowed_types' => 'gif|jpg|jpeg|png',
        'max_size' => 0,
        'filename' => url_title($this->input->post('file')),
        'encrypt_name' => false,
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload('file');

    $post=array(         
        'post'=>$this->input->post('textarea1'), 
        'image'=>$this->upload->file_name,           //image 
    ); 

    ChromePhp::log("edit data : " . json_encode($post));

    $result = $this->AdminModel->updatePost($post, $id);    
}

When checking the content of $post array using ChromePhp the 'image' shows null value. But 'post' have values.

I cannot find where I have gone wrong. Where have I gone wrong?

3 Answers 3

1

Please use this

 if ( ! $this->upload->do_upload('file'))
                {
                      echo "<pre>";print_r($this->upload->display_errors());die();

                }
                else
                {
                     echo "nothing wrong";
                }

and show me the result. If that not help, try to change

 <input type="file" name="file">

to

and change $this->upload->do_upload('file') to $this->upload->do_upload('userfile')

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

Comments

1

To upload image/file you need to add enctype attribute in you form tag

it should be like this

<form method="post" action="<?php echo site_url('adminUpdatePost/'.$b->id); ?>" class="col s12" enctype="multipart/form-data>
  <div class="row">
      <div class="input-field col s12">
          <textarea id="textarea1" name="textarea1" class="materialize-textarea"><?=$b->post?></textarea>
          <label for="textarea1">Post</label>
      </div>
  </div>

  <div class="file-field input-field">
     <div class="btn">
          <span>Post Image</span>
          <input type="file" name="file">
      </div>
      <div class="file-path-wrapper">
          <input id="image" name="image" class="file-path validate" placeholder="<?=$b->image?>" type="text">
    </div>
  </div>

  <input type="submit" value="Submit">  
</form>

Comments

0

You have to specify the "enctype="multipart/form-data"" type in form tage

<form method="post" action="<?php echo site_url('adminUpdatePost/'.$b->id); ?>" class="col s12" enctype="multipart/form-data">  

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.