Welcome Guest, Not a member yet? Register   Sign In
How to post a content without uploading image in codeigniter?
#1

[eluser]Unknown[/eluser]
I have created a form to post content. My form has image upload field to upload image. I can post a content using image properly but when I try to post a content without uploading an image, it stops me to post and show me this message: "You have not selected any file".

I am using codeigniter. how can I solve this problem.

Here is my html code:

<form name="edit_slider_form" class="form-horizontal" action="<?php echo base_url();?>super_admin/save_slider" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Edit Slider</legend>
<div class="control-group">
<label class="control-label" for="fileInput">Upload Image 1</label>
<div class="controls">
&lt;input class="input-file uniform_on" id="fileInput" type="file" name="slider_image"&gt;
</div>
</div>
<div class="control-group">
<label class="control-label" for="textarea2">Caption for Image 1</label>
<div class="controls">
&lt;textarea id="textarea2" rows="3" name="image_caption"&gt;&lt;?php echo $this->session->userdata('image_caption');?&gt;&lt;/textarea&gt;
</div>
</div>

<div class="form-actions">
<button type="submit" class="btn btn-primary" name="btn">Upload Images</button>
</div>
</fieldset>
&lt;/form&gt;

Here is my controller code:

public function save_slider() {
$data=array();
$config['upload_path'] = 'images/slider-image/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$error='';
$fdata=array();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('slider_image'))
{
$error = $this->upload->display_errors();
$sdata=array();
$sdata['error']=$error;
$sdata['image_caption']= $this->input->post('image_caption');
$this->session->set_userdata($sdata);
redirect('super_admin/add_slider');
}
else
{
$fdata = $this->upload->data();
$data['slider_image']=$config['upload_path'].$fdata['file_name'];
$sdata=array();
$sdata['message']='Image has been Uploaded';
$this->session->set_userdata($sdata);
}
$data['image_caption']= $this->input->post('image_caption');
$this->super_admin_model->save_slider_info($data);
redirect('super_admin/add_slider');
}

Here is my model code

public function save_slider_info($data) {
$this->db->insert('tbl_slider',$data);
}
#2

[eluser]Flemming[/eluser]
You need to check first whether or not an image is being uploaded:

Code:
if ($_FILES['image']['size'] !== 0) {
  if ( ! $this->upload->do_upload('slider_image'))
  {
    $error = $this->upload->display_errors();
    ... the rest of your upload code
  }

otherwise this:

Code:
if ( ! $this->upload->do_upload('slider_image'))

will always fail!
#3

[eluser]Unknown[/eluser]
[quote author="Flemming" date="1409135769"]You need to check first whether or not an image is being uploaded:

Code:
if ($_FILES['image']['size'] !== 0) {
  if ( ! $this->upload->do_upload('slider_image'))
  {
    $error = $this->upload->display_errors();
    ... the rest of your upload code
  }

otherwise this:

Code:
if ( ! $this->upload->do_upload('slider_image'))

will always fail!
[/quote]

Thanks Flemming, it works!




Theme © iAndrew 2016 - Forum software by © MyBB