如何使用文件上传表单数据,并使用以下代码在Codeigniter中插入带有上载文件名的所有表单数据

时间:2017-12-25 07:12:16

标签: php mysql codeigniter

我有一个带有文本,选择,文件字段的表单。 我正在使用Codeigniter Framework。 我想在“uploads”文件夹中上传所选文件,并插入所有带有上传文件名的表单数据。 我的代码如下。 请帮我完成这个。

查看:

<?php echo form_open_multipart('assign/task_assign'); ?>
                      <fieldset>

                        <div class="row">
                          <div class="col-md-2">Title</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <input class="form-control" name="task_title" type="text" required="required">
                            </div>
                          </div>
                        </div>

                        <div class="row">
                          <div class="col-md-2">Description</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <textarea class="form-control" cols="80" rows="7" name="task_description"></textarea>
                            </div>
                          </div>
                        </div>

                        <div class="row">
                          <div class="col-md-2">Priority</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <select class="form-control" name="task_priority" required>
                                <option value="" disabled="disabled" selected="selected">Priority
                                </option>
                                <option value="Low">Low</option>
                                <option value="Medium">Medium</option>
                                <option value="High">High</option>
                              </select>
                            </div>
                          </div>
                        </div>

                        <div class="row">
                          <div class="col-md-2">Start Date</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <input class="form-control" name="task_start" id="datepicker" type="text" required="required" autocomplete="off">
                            </div>
                          </div>
                        </div>

                        <div class="row">
                          <div class="col-md-2">Due Date</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <input class="form-control" name="task_due" id="datepicker1" type="text" required="required" autocomplete="off">
                            </div>
                          </div>
                        </div>

                        <div class="row">
                          <div class="col-md-2">Assign To</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <select class="form-control" name="task_to" value="" required>    
                                <option value="" disabled="disabled" selected="selected">To
                                </option>
                                <?php foreach ($h->result() as $row) { ?>      
                                  <option value="<?php echo $row->name; ?>"><?php echo $row->name; ?></option>
                                <?php } ?>
                              </select>
                            </div>
                          </div>
                        </div>

                        <div class="row">
                          <div class="col-md-2">Attachments</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <input type="file" class="form-control" name="userfile">
                            </div>
                          </div>
                        </div>

                        <div class="row">
                          <div class="col-md-2">Additional Information</div>
                          <div class="col-md-10">
                            <div class="form-group">
                              <textarea class="form-control" cols="80" rows="6" name="task_info"></textarea>
                              </select>
                            </div>
                          </div>
                        </div>
                          <input class="btn btn-primary btn-block" type="submit" value="Assign" required="required" name="assign">
                      </fieldset>
                  <?php echo form_close(); ?>

控制器:

public function task_assign()
{

    $name=$this->session->userdata('name');

    date_default_timezone_set('Asia/Calcutta');

    $config = array(
            'upload_path' => "./uploads/",
            'overwrite' => TRUE,
            'max_size' => "2048000",
            );

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

    $this->upload->do_upload();

    $data=array('upload_data' => $this->upload->data());

    $task=array(
            'title'=>$this->input->post('task_title'),
            'description'=>$this->input->post('task_description'),
            'priority'=>$this->input->post('task_priority'),
            'status'=>'Ongoing',
            'start_date'=>$this->input->post('task_start'),
            'due_date'=>$this->input->post('task_due'),
            'end_date'=>$this->input->post('task_due'),
            'assigned_to'=>$this->input->post('task_to'),
            'assigned_by'=> $name,
            'time'=> date('d-m-Y H:i:s'),
            'task_file' => $upload_data['file_name'],
            'additional_info'=>$this->input->post('task_info')
        );
    print_r($task);
    $assign=$this->assign_model->assign_task($task);

    if($assign)
    {
        $this->session->set_flashdata('error_msg', 'Could not Assign task');
        redirect('assign'); 
    }
    else
    {
        $this->session->set_flashdata('success_msg', 'Task Assigned to ' .$task['assigned_to']. ' Successfully...');
        redirect('assign');
    }
}

型号:

class Assign_model extends CI_model{

/* Loading Parent Construct */

function __construct()  
{  
  parent::__construct();
}

/* Get User Data From User Table */

public function fetchtable()
{
  $query = $this->db->get('user');  
  return $query;
}

/* Insert Task Into 'issues' Database */

public function assign_task($task)
{
  $this->db->insert('issues', $task);
}

}

我认为文件名应该在控制器中提供上传。但我不知道如何添加它。

此处只有文件上传和获取文件名。请帮我这样做..

0 个答案:

没有答案