Codeigniter上传库说我即使我没有上传文件也没有

时间:2017-04-20 06:33:33

标签: php codeigniter file-upload

好的,首先我需要知道上传字段是否为空。

如果上传字段为空,则数据将上传到数据库而不上传文件,但如果用户有像图像一样显示的内容,则将文件上传到文件夹,将信息上传到数据库。

但是我尝试了所有内容,它仍然说“你没有选择文件。”。

请检查我的代码:

function submit_ticket()
{
    $timezone = "Asia/Manila";
    date_default_timezone_set($timezone);

    $latestTicket = $this->TicketSubmit_m->last_ticket();

    $file_array = array();

    if(isset($_FILES) && @$_FILES['file_upload']['error'] != '4'){


        $images = $_FILES;
        $image = count($_FILES['file_upload']['name']);
        for($i=0; $i < $image; $i++){
            $_FILES['file_upload']['name'] = $images['file_upload']['name'][$i];
            $_FILES['file_upload']['type'] = $images['file_upload']['type'][$i];
            $_FILES['file_upload']['error'] = $images['file_upload']['error'][$i];
            $_FILES['file_upload']['size'] = $images['file_upload']['size'][$i];

            $this->load->library('upload');
            $this->upload->initialize($this->set_upload_options());
            $up = $this->upload->do_upload('file_upload');
            if($up === TRUE){
                $file_array[] =[
                    'cTicketNo' => $latestTicket,
                    'cImageID' => $latestTicket,
                    'ImagePath' =>  base_url() . "assets/uploads/".$_FILES['file_upload']['name']
                ];

            }else{
                $error = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('msg','<span class="login-error">'.$error['error'].'</span>');
                redirect($_SERVER['HTTP_REFERER']);
            }
        }
        $this->TicketSubmit_m->submit_ticket();
        $this->TicketSubmit_m->update_ticket_number();
        $this->TicketSubmit_m->add_image($file_array);
        $this->session->set_flashdata('msg','<span class="login-error">Ticket Successful!</span>');
        redirect($_SERVER['HTTP_REFERER']);
    }else{

        $this->TicketSubmit_m->submit_ticket();
        $this->TicketSubmit_m->update_ticket_number();          
        $this->session->set_flashdata('msg','<span class="login-error">Ticket Successful! No files uploaded</span>');
        redirect($_SERVER['HTTP_REFERER']);
    }

}

上传选项

function set_upload_options(){
    $config = array();
    $config['upload_path'] = './assets/uploads/';
    $config['allowed_types'] = 'gif|png|jpg|csv|xls|xlsx|doc|odf|odf|odp|odt|bmp';
    $config['max_size'] = '1000000';
    $config['overwrite'] = FALSE;
    return $config;
}

HTML

    <form action="<?php echo base_url();?>TicketSubmit/submit_ticket" method="POST" class="form-horizontal" enctype="multipart/form-data">
        <div class="control-group">
          <label class="control-label">Department :</label>
          <div class="controls">
            <span><?php echo $this->session->userdata['vDepartment'];?></span>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Name :</label>
          <div class="controls">
            <span><?php echo $this->session->userdata['vName'];?></span>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Username :</label>
          <div class="controls">
            <span><?php echo $this->session->userdata['cUsername'];?></span>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Category :</label>
          <div class="controls">
            <select name="vCategory">
              <option>Hardware</option>
              <option>Software</option>
              <option>Network</option>
            </select>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Concern :</label>
          <div class="controls">
            <textarea class="span11" name="vConcern" required ></textarea>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Severity :</label>
          <div class="controls">
            <select name="vLevel">
              <option>Low</option>
              <option>Medium</option>
              <option>High</option>
            </select>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Contact Person :</label>
          <div class="controls">
            <input type="text" class="span11" name="vCaller" required />
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">File upload :</label>
          <div class="controls">
            <input type="file" name="file_upload[]" multiple />
          </div>
        </div>
        <div class="control-group">
          <label class="control-label"></label>
          <div class="controls">
            <?php echo $this->session->flashdata('msg');?>
          </div>
        </div>
        <div class="form-actions">
          <button type="submit" class="btn btn-success">Send</button>
        </div>
      </form>

2 个答案:

答案 0 :(得分:1)

您是否在表单中写了enctype =“multipart / form-data”以及method =“post”?

答案 1 :(得分:0)

我简直不敢相信这个问题很简单。

我缺少一种文件类型,这就是行:

$_FILES['file_upload']['tmp_name'] = $images['file_upload']['tmp_name'][$i];