开发者

Codeigniter - How to make a file upload field non mandatory?

I have a form which has 5 file input fields, all works fine with my CRUD methods if the file field is not empty, however the client now wants to set some of the fields to be non mandatory.

This is how I am trying to do it in my code, the problem I am encountering is declaring a null variable(to insert a blank value in the respective database field) if the file field is empty. I get an undefined variable message from codeigniter when nothing is uploaded...

This is the code that code checks if a file has been uploaded or not.

if(isset($_FILES['ticketing_summary_file']))
    {
    $this->upload->initialize($config);
        if($this->upload->do_upload('ticketing_summary_file'))
        {
        $upload_data=$this->upload->data();                                
        $ticketing_summary_file_name=$upload_data['file_name'];
        $ticketing_summary_full_file_path = $path_to_uploads.'/'.$ticketing_summary_file_name;
        $show['ticketing_summary_file_url'] = $ticketing_summary_full_file_path;            
         }
    } 

    if(!isset($_FILES['ticketing_summary_fil开发者_开发百科e']))
    {       
    $show['ticketing_summary_file_url'] = $empty_file_message;                                  
    }                       

Then this is how I insert the data into my database... I have tried declaring the contents of 'ticketing_summary_file' in the code below and above, but it says its empty either way

$show = array('tour_id' =>$tour,
                        'date' => $this->input->post('date'),
                        'location' => $location);
        $id = $this->show_model->save($show);

Cheers for any help,

Dan


I ran into a similar problem. I ended up checking to see if the upload error matched the text in the lang file. There is probably a better way but it was a quick fix for me. I hope it helps.

    <?php 

    //play code file field
    $play_code_errors= '';

    $data['play_code_file_name'] = uniqid('pc_', true) . 'csv'; 

    $config['upload_path'] = './media/tmp/';
    $config['allowed_types'] = 'csv';
    $config['max_size'] = '4024';
    $config['file_name'] = $data['play_code_file_name'];

    $this->upload->initialize($config);

    if ( ! $this->upload->do_upload('play_code_file'))
    {
        $play_code_errors = $this->upload->display_errors();

        if($play_code_errors = $this->lang->line('upload_no_file_selected'))
        {
            $play_code_errors = '';
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜