CodeIgniter图片上传-无法显示错误消息 [英] CodeIgniter Image Upload - can't get error message to show

查看:61
本文介绍了CodeIgniter图片上传-无法显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的上传模型

 函数upload_avatar()
{
$ id = $ this -> tank_auth-> get_user_id();

//配置上传参数并上传图像
$ config = array(
'allowed_types'=>'jpeg | jpg | png',
'upload_path' => $ this-> upload_path,
'max_size'=> 2048,
'encrypt_name'=>是,
'overwrite'=>否,
);
$ this-> load-> library('upload',$ config);
$ this-> upload-> do_upload();

//获取上传数据,配置,调整上传图像的大小,保存在头像子文件夹
$ image_data = $ this-> upload-> data();

if($ image_data ['file_size']< 2048){

$ config = array(
'source_image'=> $ image_data ['full_path '],
'new_image'=> $ this-> upload_path。'/ avatars',
'maintain_ratio'=> TRUE,
'width'=> 125,
'height'=> 125
);
$ this-> load-> library('image_lib',$ config);
$ this-> image_lib-> resize();

//只有在没有上传错误的情况下才烧录头像到user_profiles表
if(!$ this-> upload-> display_errors()){
$ data = array ('avatar'=> base_url()。'images / avatars /'。$ image_data ['file_name']));
$ this-> db-> where('id',$ id);
$ this-> db-> update('user_profiles',$ data);
}

//从服务器
中删除原始文件$ this-> load-> helper(’file’);
unlink($ image_data [’full_path']);

} else {

echo $ this-> upload-> display_errors();

}
}

我找不到错误



公平地说,CI会忽略此大文件,而当文件小于<时正确上传。 2MB。



唯一的是我无法让错误消息显示在前端,无法为suer提供一些反馈。



有什么想法吗?

解决方案

  $ config ['upload_path'] ='上传/类别/'.$id.'/'; 
//回显$ file_name; die;
// echo $ config [’upload_path’];
$ config [’allowed_types’] =‘gif | jpg | jpeg | png’;
$ config ['max_size'] ='2048';
$ config [‘max_width’] =‘1920’;
$ config [‘max_height’] =‘1280’;
$ this-> load-> library(‘upload’);
foreach($ _FILES as $ key => gt; $ value){
// print_r($ key);

if(!empty($ key [’name’])){

$ this-> upload-> initialize($ config);
if(!$ this-> upload-> do_upload($ key)){
// echo‘test’; die;
// rmdir(’uploads / category /’.$ id);
$ errors = $ this-> upload-> display_errors();
flashMsg($ errors);
}
}
}

尝试一下! p>

This is my upload model

    function upload_avatar()
    {
        $id = $this->tank_auth->get_user_id();

        //config upload parameters and upload image
        $config = array(
            'allowed_types' => 'jpeg|jpg|png',
            'upload_path' => $this->upload_path,
            'max_size' => 2048,
            'encrypt_name' => TRUE,
            'overwrite' => FALSE,
        );
        $this->load->library('upload', $config);
        $this->upload->do_upload();

        //get upload data, config, resize uploaded image, save in avatars subfolder
        $image_data = $this->upload->data();

        if ($image_data['file_size'] < 2048) {

            $config = array(
                'source_image' => $image_data['full_path'],
                'new_image' => $this->upload_path . '/avatars',
                'maintain_ratio' => TRUE,
                'width' => 125,
                'height' => 125
            );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();

            //only burn avatar path to user_profiles table if no upload errors
            if (!$this->upload->display_errors()) {
                $data = array('avatar' => base_url() . 'images/avatars/' . $image_data['file_name']);
                $this->db->where('id', $id);
                $this->db->update('user_profiles', $data);
            }

            //delete the original file from server
            $this->load->helper('file');
            unlink($image_data['full_path']);

        } else {

    echo $this->upload->display_errors();

        }
    }

I can't get the error message to echo straight to the browser when I try uploading a file > 2MB.

To be fair, CI ignores this large file, and uploads correctly when a file is < 2MB.

The only thing is that I can't get the error message to show on the front-end to give the suer some feedback.

Any ideas what's wrong here?

解决方案

$config['upload_path'] = 'uploads/category/'.$id.'/';
        //echo $file_name;die;
        //echo $config['upload_path'];
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '2048';
        $config['max_width'] = '1920';
        $config['max_height'] = '1280';
        $this->load->library('upload');
         foreach ($_FILES as $key => $value) {
            //print_r($key);

            if (!empty($key['name'])) {

                $this->upload->initialize($config);
                if (!$this->upload->do_upload($key)) {
                  // echo 'test';die;
//                    rmdir('uploads/category/'.$id);
                    $errors = $this->upload->display_errors();
                    flashMsg($errors);
                }
}
}

try this!!

这篇关于CodeIgniter图片上传-无法显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆