CodeIgniter上传类允许的文件类型

时间:2015-01-20 20:55:18

标签: php codeigniter file-upload

我正在使用Codeigniter上传类,但我对允许的文件类型有疑问。我只需要允许上传pdf文件,但可以上传任何类型的文件。这是我的代码;

模型(muser.php);

function cv_ekle()
{
    $config['upload_path'] = 'uploads/cv';
    $config['allowed_types'] = 'pdf';
    $config['max_size'] = '0';
    $config['max_width']  = '0';
    $config['max_height']  = '0';
    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $data = $this->upload->data();

    $this->load->library('upload', $config);
    $upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
    $file_name = $upload_data['file_name'];
    $data = array
        (
            'userid' => $this->session->userdata('id'),
            'kullanici' => $this->session->userdata('isim'),
            'kategori' => $this->input->post('kategori'),
            'tarih' => time(),
            'dosya' => $file_name
        );
    if($this->db->insert('cv', $data))
    {
        return true;
    }
    else
    {
        return false;
    }
}

controller(cv.php);

function cv_ekle()
{
    if($this->muser->cv_ekle())
    {
        $this->session->set_flashdata('ok', 'CV başarıyla gönderildi!');
        redirect('cv');
    }
    else
    {
        $this->session->set_flashdata('hata', 'Sadece PDF, Excel ya da Word formatında yükleme yapabilirsiniz!');
        redirect('cv');
    }
}

view(cv.php);

<form method="post" action="<?php echo site_url('cv/cv_ekle'); ?>" class="login" enctype="multipart/form-data">
                    <div class="controls">
                        <label for="email">Dosya: <span class="text-error">*</span></label>
                        <input type="file" id="pass" class="input-block-level" name="userfile" >
                    </div>
                    <div class="controls">
                        <label for="email">Kategori: <span class="text-error">*</span></label>
                        <select class="input-block-level" name="kategori">
                            <?php foreach($kategoriler as $kat) { ?>
                                <option value="<?php echo $kat['isim']; ?>"><?php echo $kat['isim']; ?></option>
                            <?php } ?>
                        </select>
                    </div>
                    <div class="controls">

                        <button type="submit" class="btn btn-primary">CV Yükle</button>
                    </div>
                </form>

提前致谢!

1 个答案:

答案 0 :(得分:0)

    function cv_ekle()
{
    $config['upload_path'] = 'uploads/cv';
    $config['allowed_types'] = 'pdf';
    $config['max_size'] = '0';
    $config['max_width']  = '0';
    $config['max_height']  = '0';
    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
    $file_name = $upload_data['file_name'];
    $data = array
        (
            'userid' => $this->session->userdata('id'),
            'kullanici' => $this->session->userdata('isim'),
            'kategori' => $this->input->post('kategori'),
            'tarih' => time(),
            'dosya' => $file_name
        );
    if($this->db->insert('cv', $data))
    {
        return true;
    }
    else
    {
        return false;
    }
}