无法上传文件(不选择要上传的文件)

时间:2015-11-24 08:49:54

标签: codeigniter image-uploading

我想进行表单编辑。在编辑表单中,我将上传照片。但是当单击提交按钮时,则会出现错误:您没有选择要上载的文件。问题是什么? 控制器:

public function edit_data($id_artikel='',$foto=''){
    $artikel = $this->mymodel->getedit($id_artikel);
    $data = array(
        "id_artikel"        => $artikel[0]['id_artikel'],
        "judul"             => $artikel[0]['judul'],
        "kategori_terpilih" => $artikel[0]['nama_kategori'],
        "isi"               => $artikel[0]['isi'],
        "foto"              => $artikel[0]['foto']
    );
    $kategori = $this->mymodel->get_kategori();
    $a['kategori'] = $kategori->result();
    $this->load->view('form_edit',$data);
    }

public function do_update(){
        $config['upload_path'] = 'images/uploaded/';
        $config['allowed_types'] ='gif|jpg|png';

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

        if(!$this->upload->do_upload()){
            $sub_data['error']=$this->upload->display_errors();
            echo '<pre>';
            print_r($sub_data);
            echo '</pre>';

            die;    
        }else{
            $result_upload=$this->upload->data();
        }

    $id_artikel     = $_POST['id_artikel'];
    $judul          = $_POST['judul'];
    $nama_kategori  = $_POST['nama_kategori'];
    $isi            = $_POST['isi'];
    $foto           = $result_upload['file_name']; 
    $data_insert    = array(
    'id_artikel'    => $id_artikel,
    'judul'         => $judul,
    'nama_kategori' => $nama_kategori,
    'isi'           => $isi,
    'foto'          => $foto);
    $where = array('id_artikel' => $id_artikel);
    $res = $this->mymodel->UpdateData('artikel',$data_update,$where);
    if($res>=1){
        $this->session->set_flashdata('pesan','Update Data Sukses');
        redirect('crud/index');
    }
    }

模特:

public function getedit($id_artikel=''){
    $data = $this->db->query('SELECT a.id_artikel, a.judul, a.tanggal_update,a.isi, a.foto, b.nama_kategori FROM artikel as a LEFT JOIN kategori as b on a.id_kategori=b.id_kategori where id_artikel = ' .$id_artikel);
    return $data->result_array();
}
public function UpdateData($tabelNama,$data,$where){
    $res = $this->db->update($tabelNama,$data,$where);
    return $res;
    }

观点:

<html>
<head>
    <title>Data Mahasiswa</title>
</head>
<body>
    <form method="POST" action="<?php echo base_url()."index.php/crud/do_update"; ?>">
   <table>
        <tr>
            <td>Id Artikel</td>
            <td><input type="text" name="id_artikel" value="<?php echo $id_artikel; ?>" readonly/></td>
        </tr>
        <tr>
            <td>Judul</td>
            <td><textarea name="judul"><?php echo $judul; ?></textarea></td>
        </tr>
        <tr>
            <td>Kategori</td>
            <td><label>
            <select name="kategori" class="textfield" id="id_kategori">
            <option id="0">--Pilih Kategori--</option>
            <?php
            foreach ($kategori as $a) { ?>
            <option <?php if($kategori_terpilih == $a->id_kategori) echo 'selected = "selected"'; ?> value="<?php echo $a->id_kategori ?>" <?php echo $a->nama_kategori ?>></option>;   
            <?php
            }
            ?>
        </select>  
        </tr>
        <tr>
            <td>Isi</td>
            <td><textarea name="isi"><?php echo $isi ?></textarea></td>
        </tr>
        <tr>
            <td>Foto</td>
            <td><input type="file" name="foto" size="20" /><?php echo $foto ?></td>
        </tr>
        <tr>
            <td>
            </td>
            <td><input type="submit" name="btnSubmit" value="Simpan"/></td>
        </tr>

    </table>
    </form>
</body>

1 个答案:

答案 0 :(得分:4)

来自CodeIgniter&#39; documentation

  

默认情况下,上传例程期望文件来自表单   字段名为userfile,表单必须是“multipart”类型。

因此,在文件上传字段中使用name="userfile"作为name属性,或使用$this->upload->do_upload('foto')指定字段

同时将enctype="multipart/form-data"放在您的表单上。