我怎样才能使用代码点火器上传图像文件?

时间:2016-07-08 04:30:14

标签: php codeigniter

嗨我正在尝试使用代码igniter上传图像文件。我只需要上传png,jpg,jpeg文件。但是其他类型的文件也上传了像.txt,.mp3等。我的代码如下。请参阅帮助我立即解决这个问题。谢谢所有人提前。

    $config['upload_path'] = './avatar/';
    $config['allowed_types'] = 'gif|jpeg|png|jpg';
    $config['max_size'] = '1024mb';
    $config['max_width'] = '3000';
    $config['max_height'] = '3000';
    $this->load->library('upload', $config);

    if (!$this->upload->do_upload()) {

        $ve['data'] = $this->upload->display_errors();
    } else {
        $this->upload->data();
    } 

2 个答案:

答案 0 :(得分:1)

    $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);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the //class:
$this->upload->initialize($config);

if($this->upload->do_upload())
{
  $data = array('upload_data' => $this->upload->data());
  $this->load->view('upload_success',$data);
}else
{
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('file_view', $error);
}

答案 1 :(得分:0)

我认为您需要在尝试在if条件下验证它时发送用户文件/上传文件

if ( ! $this->upload->do_upload('userfile'))
{
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('upload_form', $error);
}
else
{
    $this->upload->data();
}