CodeIgniter Forums
Form Data and File Upload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Form Data and File Upload (/thread-25087.html)

Pages: 1 2 3


Form Data and File Upload - El Forum - 11-30-2009

[eluser]wowdezign[/eluser]
Does anyone have any recommendations on how to use the file upload class with form data when the file upload is not required?

I have a form with 6 form fields. Five are text and one is the file field.

I don't want the the file field to be required. However, when I submit the form I get the error stating that there is no file to upload all by itself and no other form errors show.

Any suggestions?


Form Data and File Upload - El Forum - 11-30-2009

[eluser]Jeremy Gimbel - Conflux Group[/eluser]
Could you post your code? I have some ideas, but I'd need to see how you're doing it to test them.

jeremy


Form Data and File Upload - El Forum - 11-30-2009

[eluser]wowdezign[/eluser]
controller function:
Code:
function add(){
    $this->load->library('form_validation');
    $this->load->helper('form');
    $this->form_validation->set_error_delimiters('<div class="errorDiv">','</div>');
    $this->form_validation->set_rules('news_title','Title','required|trim|max_length[63]|xss_clean');
    $this->form_validation->set_rules('active','Active','required|trim|exact_length[1]|xss_clean');
    $this->form_validation->set_rules('news_desc','Description','required|trim|max_length[255]|xss_clean');
    $this->form_validation->set_rules('news_body','Body','required|trim|max_length[65000]|xss_clean');
    $this->form_validation->set_rules('news_month','Month','required|trim|xss_clean');
    $this->form_validation->set_rules('news_date','Date','required|trim|xss_clean');
    $this->form_validation->set_rules('news_year','Year','required|trim|xss_clean');        
    if($this->form_validation->run()==FALSE){            
        $data['token'] = md5(uniqid(mt_rand(),true));
        $this->session->set_userdata('token',$data['token']);
        $data['title'] = $this->Page->title;
        $data['desc'] = $this->Page->desc;
        $data['kw'] = $this->Page->kw;
        $data['body'] = $this->Page->body;
        $data['menu'] = $this->Page->setMenu();
        $data['subMenu'] = $this->Page->childPages;
        $data['newsList'] = $this->MNews->getNewsByUserID($this->session->userdata('user_id'));
        $data['contentView'] = 'admin/news_add';
        $this->load->view('template',$data);
    }else{            
//        Process the submitted form here .......
        if(!(
            ($this->session->userdata('token')==$this->input->post('token')) &&
            ($this->input->post('submit')=='Save News Item'
        ))){
            $this->session->set_flashdata('fail_string','OOPS! There was problem with the form you submitted.');
            redirect('main/logout','refresh');
        }
        $data['token'] = $this->input->post('token');
        $config['upload_path']='./photos/news/';
        $config['allowed_types']='gif|jpg|png';
        $config['max_size']='2000';
        $config['max_width']='5000';
        $config['max_height']='5000';
        $config['remove_spaces']=TRUE;
        $this->load->library('upload', $config);        
        if (!($this->upload->do_upload())){
            $this->session->set_flashdata('fail_string',$this->upload->display_errors());
            redirect('admin/news/add');
        }else{
            $data = array('upload_data'=>$this->upload->data());
            rename($config['upload_path'].$data['upload_data']['file_name'],$ni=$config['upload_path'].md5(date('U').$_SERVER['REMOTE_ADDR']).".jpg");            
            $config['image_library']='gd2';
            $config['source_image']=$ni;
            $config['create_thumb']=FALSE;
            $config['maintain_ratio']=TRUE;
            $config['width']=350;
            $config['height']=350;
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            $data['title']=$this->Page->title;
            $data['desc']=$this->Page->desc;
            $data['kw']=$this->Page->kw;
            $data['body']=$this->Page->body;
            $data['menu']=$this->Page->setMenu();
            $data['subMenu']=$this->Page->childPages;
            $data['newsList']=$this->MNews->getNewsByUserID($this->session->userdata('user_id'));
            $data['contentView']='admin/news_add';
            $this->load->view('template',$data);
        }        
    }
}



Form Data and File Upload - El Forum - 11-30-2009

[eluser]wowdezign[/eluser]
And the form code:
Code:
echo validation_errors();
if($this->session->flashdata('fail_string')){$this->load->view('error_div');}
if($this->session->flashdata('message_string')){$this->load->view('message_div');}
$date_month = array('1'=>'January','2'=>'February','3'=>'March','4'=>'April','5'=>'May','6'=>'June','7'=>'July','8'=>'August','9'=>'September','10'=>'October','11'=>'November','12'=>'December');
for($i=1;$i<=31;$i++){$date_date[$i]=$i;}
$yearb = date('Y',strtotime('-5 year'));
for($i=1;$i<=15;$i++){$date_year[$yearb]=$yearb;$yearb++;}
$hidden = array('token'=>$token,'id'=>'','user_id'=>$this->session->userdata('user_id'),'date_added'=>date('Y-m-d H:i:s'));
$atts = array('id' => 'addNewsForm');
echo form_open(site_url('admin/news/add'),$atts,$hidden);
echo"<table class='formTable'><tr><td>";
$nt = array('name'=>'news_title','id'=>'news_title','size'=>'70','value'=>set_value('news_title'));
echo form_label('Title: (63 Characters or less)','news_title')."<br/>";
echo form_input($nt)."<br/><br/>";
if(!(set_value('active'))){
    $actcheckno=FALSE;
    $actcheckyes=TRUE;
}else{
    $actcheckyes=FALSE;
    $actcheckno=TRUE;    
}
$actn=array('name'=>'active','id'=>'active_0','checked'=>$actcheckno,'value'=>0);
$acty=array('name'=>'active','id'=>'active_1','checked'=>$actcheckyes,'value'=>1);
echo"Activate this news item? ";
echo form_label('Yes:','active_1');
echo form_radio($acty,$acty['value'],set_radio($acty['name'],$acty['value'],$actcheckyes));
echo form_label('No:','active_0');
echo form_radio($actn,$actn['value'],set_radio($actn['name'],$actn['value'],$actcheckno))."<br/><br/>";
echo form_label('Date: (The date you want the activated item to begin showing)')."<br/>";
echo form_dropdown('news_month',$date_month,set_value('news_month',date('m')))." ";
echo form_dropdown('news_date',$date_date,set_value('news_date',date('d')))." ";
echo form_dropdown('news_year',$date_year,set_value('news_year',date('Y')))."<br/><br/>";
$npu=array('name'=>'userfile','id'=>'userfile','size'=>'60','value'=>set_value('userfile'));
echo form_label('Image to show: ');
echo form_upload($npu)."<br/><br/>";
$nd=array('name'=>'news_desc','id'=>'news_desc','rows'=>'4','value'=>set_value('news_desc'));
echo form_label('Description: (A summarization of the news item less than 255 characters)','news_desc')."<br/>";
echo form_textarea($nd)."<br/><br/>";
$nb=array('name'=>'news_body','id'=>'news_body','value'=>set_value('news_body'));
echo form_label('Body: (The news item contents up to 65,000 characters)','news_body')."<br/>";
echo form_textarea($nb)."<br/><br/>";
echo"</td></tr><tr><td colspan='2' class='buttonCell'>";
echo"<br/>";
echo form_submit('submit','Save News Item');
echo"</td></tr></table>";
echo form_close();



Form Data and File Upload - El Forum - 12-01-2009

[eluser]Genjo[/eluser]
I have same problem. I don’t want the the file field to be required. But I alway get problem with error message "You did not select a file to upload."


Form Data and File Upload - El Forum - 12-01-2009

[eluser]umefarooq[/eluser]
hi first of all you have to make your form multipart right now in form code you are using

Code:
form_open(action);

change it to multipart if you are using file field type.
Code:
form_open_multipart(action);

please check user guide
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html


Form Data and File Upload - El Forum - 12-01-2009

[eluser]wowdezign[/eluser]
Thanks for the comments and help. I appreciate it. I recently changed it to form_open to test something out and hadn't changed it back before posting the code.

However, that's not the cause of the issue I have described. It doesn't work with the multipart open function either.


Form Data and File Upload - El Forum - 12-01-2009

[eluser]überfuzz[/eluser]
Code:
if(!empty($_FILES))
{
   //run upload
}



Form Data and File Upload - El Forum - 12-01-2009

[eluser]wowdezign[/eluser]
Genjo, you know what I am talking about then. It just doesn't make any sense that the file upload would have to be used by itself.

It works great by itself, however, if you look in the documentation, there isn't an example of how to use it in a form with other fields. It seems to be strictly for uploading files not part of another form.


Form Data and File Upload - El Forum - 12-01-2009

[eluser]wowdezign[/eluser]
Thanks überfuzz, I'll give it a shot and see if that solves the problem.