上传图片文件时出错

时间:2017-04-18 14:42:33

标签: php codeigniter

我正在尝试制作用户个人资料并在codeigniter中添加个人资料图片功能,我简单地复制粘贴文档中给出的文件上传代码,但它显示错误。我的视图文件是

 <?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

我的控制器是

<?php

class Upload extends CI_Controller {

    public function __construct()
    {
            parent::__construct();
            $this->load->helper(array('form', 'url'));
            $this->load->library('upload');
    }



    public function do_upload()
    {
            $config['upload_path']          = './images/';
            $config['allowed_types']        = 'gif|jpg|jpeg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

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

            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors());

                    echo'not happening';
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $this->load->view('upload_success', $data);
            }
    }
}
?> 

它显示错误在这里我的意思是它回应&#39;没有发生&#39;,这意味着它没有上传。如果有人可以请帮助我,它会很棒。

2 个答案:

答案 0 :(得分:1)

为什么不检查确切的错误是什么?我刚刚更新了代码,以跟踪codeigniter返回的确切错误。

if ( ! $this->upload->do_upload('userfile'))
{
   $error = array('error' => $this->upload->display_errors());
   echo '<pre>';
   print_r($this->upload->display_errors());
}

答案 1 :(得分:0)

你已经加载了两次库有时我发现你在构造函数中放置了一个原因问题而在函数中放了一个

尝试如下并在函数中使用$this->upload->initialize($config);并在__constructor中加载库。

https://www.codeigniter.com/user_guide/libraries/file_uploading.html#preferences

<?php

class Upload extends CI_Controller {

    public function __construct()
    {
            parent::__construct();
            $this->load->helper(array('form', 'url'));
            $this->load->library('upload');
    }

    public function do_upload()
    {
            $config['upload_path'] = './images/';
            $config['allowed_types']  = 'gif|jpg|jpeg|png';
            $config['max_size'] = 5000;
            $config['max_width'] = 0;
            $config['max_height']  = 0;

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors());

                    echo'not happening';
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    $this->load->view('upload_success', $data);
            }
    }
}
  

请注意,不需要使用?>关闭codeigniter控制器和模型   在用户指南

中说
application

// Where you upload images to

images

system 

index.php