Codeigniter validate_upload库无法上传文件

时间:2013-09-28 09:34:33

标签: php codeigniter validation upload

我被{@ 3}}上的@raheel shan的帖子引用并使用此this page扩展库对输入文件进行if(!$this->upload->validate_upload('photo'))验证,如果用户尝试上传无效,验证工作正常文件类型和消息只显示在正确的位置。

不知何故,我无法将图像文件上传到其文件夹,并且文件名无法保存到表格中,在使用此库之前,上传图像文件并保存到表格非常有效。

有人可以帮忙解决我的代码中有什么问题吗?

控制器:

public function index()
{
    if(!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login');

    }else{

        $user_id = $this->session->userdata('user_id');
        $profile = $this->users->get_profile_by_id($user_id);
        $checked = $this->input->post('remove');

        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|min_length[5]|max_length[30]');
        $this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
        $this->form_validation->set_rules('website', 'Website', 'trim|xss_clean|max_length[30]');


        if(isset($_FILES['photo']) AND !empty($_FILES['photo']['name'])) {

            $this->form_validation->set_rules('photo', 'Photo', 'trim|callback_valid_upload');

            if($this->upload->do_upload('photo')){

                // image resizing
                $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 100;
                $config['height'] = 100;

                $this->load->library('image_lib', $config); //codeigniter default function

                if(!$this->image_lib->resize()){

                    $this->session->set_flashdata('upload_msg', $this->image_lib->display_errors());

                }else{

                    $image_data = $this->upload->data();
                    $photo_to_table = $image_data['file_name'];
                }

            }

        }else{

            if((int) $checked == 1){

                unlink('./uploads/'.$profile->photo);
                $photo_to_table = '';

            }else{
                $photo_to_table = $profile->photo;
            }
            //$photo_to_table = ((int) $checked == 1) ? '' : $profile->photo;
        }

        if($this->form_validation->run()) { // validation ok

            if(!is_null($data = $this->tank_auth->update_user(
                $this->form_validation->set_value('name'),
                $this->form_validation->set_value('country'),
                $this->form_validation->set_value('website'),
                $photo_to_table
            ))) { // success

                $this->session->set_flashdata('msg', 'Profile has been updated');
                redirect(current_url());
            }

        }else{

            $errors = $this->tank_auth->get_error_message();
            foreach($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
        }

//blah blah..//load view

}

public function valid_upload()
{
    $user_id = $this->session->userdata('user_id');

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '500';
    $config['file_name'] = $user_id.'_'.strtotime('now');
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

    if(!$this->upload->validate_upload('photo'))
    {
        $this->form_validation->set_message('valid_upload', $this->upload->display_errors());
        return FALSE;
    }else{
        return TRUE;
    }
}


 }

感谢。

2 个答案:

答案 0 :(得分:1)

我有类似的问题,在我的情况下,这是因为我选择了错误的图像库。检查您的PHP安装以获取您想要使用的相应图像库,并确保它匹配。默认情况下,CodeIgniter选择GD2(GD库版本> = 2)作为默认图像库 http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

试试这个:

// image resizing
$config['image_library'] = 'gd'; // specify the correct image library
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;

答案 1 :(得分:0)

中的结果是什么?
$this->image_lib->display_errors()

您的上传目录是否可写?

你在GD中安装了GD吗?也许像马哈茂德建议的那样尝试GD?