Reputation: 622
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?
Upvotes: 0
Views: 59
Reputation: 46
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>
Upvotes: 1
Reputation: 186
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">
Upvotes: 0
Reputation: 1202
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')
Upvotes: 1