codeigniter表单发送文件为null

时间:2016-02-18 13:41:05

标签: php forms codeigniter

我尝试使用codeigniter以这种方式发送图像:

<?php echo form_open_multipart('noticia/inserir', 'id="form-pessoas"'); ?>

<label for="titulo">Título:</label><br/>
<input type="text" name="titulo" value="<?php echo set_value('titulo'); ?>"/>
<div class="error"><?php echo form_error('resumo'); ?></div>

<label for="resumo">Resumo:</label><br/>
<input type="text" name="resumo" value="<?php echo set_value('resumo'); ?>"/>
<div class="error"><?php echo form_error('resumo'); ?></div>

<label for="descricao">Descrição:</label><br/>
<input type="text" name="descricao" value="<?php echo set_value('descricao'); ?>"/>
<div class="error"><?php echo form_error('descricao'); ?></div>

<label for="data">Data</label><br/>
<input type="text" name="data" value="<?php echo set_value('data'); ?>"/>
<div class="error"><?php echo form_error('data'); ?></div>

<label for="Foto">Foto:</label>
<input type="file" name="foto" id="foto" size="20" value="<?php echo set_value('foto'); ?>"/>

<input type="submit" name="cadastrar" value="Cadastrar" />

<?php echo form_close(); ?>

但输入文件发送为空。

我的控制器:

function inserir()
{

    $this->load->library('form_validation');
    $this->load->helper('form');
    $this->load->helper('url');


    $this->form_validation->set_error_delimiters('<span>', '</span>');


    $this->form_validation->set_rules('titulo', 'Titulo', 'required|max_length[60]');
    $this->form_validation->set_rules('resumo', 'Resumo', 'required|max_length[100]');
    $this->form_validation->set_rules('descricao', 'Descrição', 'required|max_length[100]');
    //$this->form_validation->set_rules('data', 'Data', 'required');


    if ($this->form_validation->run() === FALSE) {
        $this->index();

    } else {

        $data['ds_titulo'] = $this->input->post('titulo');
        $data['ds_resumo'] = $this->input->post('resumo');
        $data['ds_noticia'] = $this->input->post('descricao');
        $data['ds_foto'] = $this->input->post('foto');
        //$data['data'] = $this->input->post('data');
        var_dump($this->input->post('foto'));exit;

        $this->upload($data);


        if ($this->model->inserir($data)) {
            redirect('noticia');
        } else {
            log_message('error', 'Erro ao inserir a noticia.');
        }
    }
}



  function upload($data){

    var_dump($data);exit;
    $this->load->helper('form');
    $this->load->helper('url');


    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if ( ! $this->upload->do_upload($data['foto']))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('cadastro-noticia', $error);
    }
    else
    {
        return true;


    }
}

需要获取图像文件并上传然后将数据记录在数据库中,但问题是从表单发送数据开始,任何人都可以帮忙吗?感谢。

1 个答案:

答案 0 :(得分:0)

 var_dump($_FILES['foto']);exit;

和信息你不能通过任何东西设置文件输入值..

<input type="file" name="foto" id="foto" size="20" value="<?php echo set_value('foto'); ?>"/>

SO这里的value属性没有用......:)

<input type="file" name="foto" id="foto" size="20" />