0

我在此页面上被提及@raheel shan 的线程并使用此My_Upload扩展库来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;
    }
}


 }

谢谢。

4

2 回答 2

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;
于 2013-09-28T16:04:17.647 回答
0

你的结果是什么

$this->image_lib->display_errors()

你的上传目录是可写的吗?

你在 php 中安装并激活了 GD 吗?也许按照 Mahmoud 的建议尝试 GD?

于 2013-09-29T15:33:25.890 回答