php - 在codeigniter中上传图片

标签 php codeigniter

我正在尝试使用以下代码上传图像: Controller :

public function do_register()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('uname', 'Username', 'required|min_length[4]|max_length[15]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('pass', 'Password', 'required|min_length[4]');
        $this->form_validation->set_rules('address', 'Details', 'required|min_length[4]');
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('login_view');
        }
        else
        {
            $path = $_FILES['image']['name'];
            $imgext=strtolower(strrchr($path,'.'));
            $imgname= $this->generateRandomString().$imgext;
            if($path!='')
            {
                $im= $this->config->item('base_url').'/uploads'.'/'.$imgname;
                $x=$this->do_upload($imgname);
                $data['img']=$im;
            }

            $this->search_model->register_user($data['img']);
            $this->load->view('register_view'); 
        }
    }   
    function generateRandomString()
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $randomString = '';
        for ($i = 0; $i < 8; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }   
    function do_upload($img)
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';

        $config['max_size'] = '1024 ';

        $config['file_name'] = $img;
        $this->load->library('upload',$config);

        if ( ! $this->upload->do_upload('image'))

        {
            $error = array('error' => $this->upload->display_errors()); 
            print_r($error);
             die();
            register("search/register");
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $this->load->view(register_view,$data);
            return $data;
        }
        return;
    }

我上传的图像大小大于 1 MB,进行注册后我没有收到错误消息。但是当我使用代码 print_r($error) 打印错误时,

error message is displayed as "The uploaded file exceeds the maximum allowed size in your PHP configuration file."

如何解决这个问题?

最佳答案

听起来您需要增加 php.ini 中的 post_max_size。还增加了 upload_max_filesize

php.ini

设置

post_max_size = 256M// according to your requirment
upload_max_filesize 128M

关于php - 在codeigniter中上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31718102/

相关文章:

html里面的php代码被注释掉了

php - 检查登录状态 JavaScript

php - 将表 1 中的列值定义为表 2 中的值

php - stream_get_contents 是否比 file_get_contents 级别更低且速度更快?

php - 试图在 php 中更新 mysql 数据库

php - 文件未使用 html 输入类型文件上传

php - 用于获取编号数组的 native CodeIgniter 函数(PDO 驱动程序)?

php - 找不到 CodeIgniter 模型

javascript - 使用 javascript 更改下拉列表的值

PHP/MySQL : Date lower than today