You did not select a file to upload

时间:2016-02-03 03:03:08

标签: php codeigniter

I'm building simple app

This my controller

function post() {
    $config['file_name'] = 'produk_' . date('Y_m_d_H_i_s');
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
    $config['max_size'] = '8000';
    $config['max_width'] = '4000';
    $config['max_height'] = '2000';
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if (!$this->upload->do_upload()) {
        echo $this->upload->display_errors();
        echo 'Error Bos';
    } else {
        if ($this->_validation() === FALSE) {
            $this->session->set_flashdata('message', $this->message->message_error('Gagal Menambah Data'));
            redirect(base_url() . "admin/produk");
        } else {
            $data = $this->upload->data();
            $this->_create_thumbnail($data["file_name"]);
            $this->_create_mini($data["file_name"]);
            $data_adalah = $this->input->post();
            $data_adalah['GambarBesar'] = $data['file_name'];

            unset($data_adalah['submit']);
            $this->mproduk->add_record($data_adalah);
            $this->session->set_flashdata('message', $this->message->message_success('Berhasil Menambah Data'));
            redirect(base_url() . "admin/produk");
        }
    }
}

and this my view

<?php echo form_open_multipart('admin/produk/post'); ?>

<table style="width:500px">
   <thead>
      <tr>
         <th colspan="2" class="left">Administrator Information Update</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td width="219" class="title">Kode Produk</td>
         <td width="219" class="price" style="text-align:left;"><?php echo form_input(array('name'=>'kodeproduk','size'=>'40')); ?></td>
      </tr>
      <tr>
         <td>Stok</td>
         <td><?php
         foreach ($size as $size_) {
             echo form_input(array('name' => 'ukr_' . $size_['id'], 'size' => '20', 'placeholder' => $size_['ukuran_produk']));
         }
         ?></td>
      </tr>
      <tr>
         <td width="219" class="title">Nama Kategori</td>
         <td width="219" class="price" style="text-align:left;"><div class="select">
               <select name="id_kategori" id="drop_1">
                  <option disabled="disabled" selected="selected">-Pilih Kategori-</option>
                  <?php foreach ($cats as $value) {?>
                  <option value="<?php echo $value->id_kategori; ?>"> <?php echo $value->namaKategori ?> </option>
                  <?php } ?>
               </select>
               <span id="wait_1" style="display: none;"> <img alt="Please Wait" src="<?php echo base_url()."img/"; ?>ajax-loader.gif"/> </span> <span id="result_1" style="display: none;"></span> </div></td>
      </tr>
      <tr>
         <td width="219" class="title">Jenis Kelamin</td>
         <td width="219" class="price" style="text-align:left;"><div class="select">
               <select name="id_human" id="drop_1">
                  <option disabled="disabled" selected="selected">-Pilih Kategori-</option>
                  <?php foreach ($human as $value) {?>
                  <option value="<?php echo $value->id_human; ?>"><?php echo $value->namaHuman ?></option>
                  <?php } ?>
               </select>
               <span id="wait_1" style="display: none;"> <img alt="Please Wait" src="<?php echo base_url()."img/"; ?>ajax-loader.gif"/> </span> <span id="result_1" style="display: none;"></span> </div></td>
      </tr>
      <tr>
         <td width="219" class="title">Nama Produk</td>
         <td width="219" class="price" style="text-align:left;"><?php echo form_input(array('name'=>'namaproduk','size'=>'40')); ?></td>
      </tr>
      <tr>
         <td width="219" class="title">Harga</td>
         <td width="219" class="price" style="text-align:left;"><?php echo form_input(array('name'=>'harga','size'=>'40')); ?></td>
      </tr>
      <tr>
         <td width="219" class="title">Berat</td>
         <td width="219" class="price" style="text-align:left;"><?php echo form_input(array('name'=>'berat','size'=>'20')); ?> .KG </td>
      </tr>
      <tr>
         <td width="219" class="title">Gambar</td>
         <td width="219" class="price" style="text-align:left;"><input type="file" name="userfile" size="20" /></td>
      </tr>
      <tr>
         <td width="219" class="title">Keterangan</td>
         <td width="219" class="price" style="text-align:left;"><div class="textarea textarea-editor">
               <textarea id="textarea" name="ket" cols="50" rows="5" class="editor"></textarea>
            </div></td>
      </tr>
      <tr>
         <td colspan="2" class="title"><button style="bottom:-38px; left: 53%;" style="font-size: 20px;">
            <span>Monyet</span>
            </button></td>
      </tr>
   </tbody>
</table>
<?php echo form_close(); ?>

I have a proble when I'm trying to upload an image, I'm always got an error "You di not select a file to upload." What wrong in my code ? Help me sir

1 个答案:

答案 0 :(得分:0)

尝试将$this->upload->do_upload()更改为$this->upload->do_upload('userfile')