codeigniter在上传文件时出错?

时间:2013-04-05 22:00:02

标签: php mysql codeigniter

我正在尝试在我的CodeIgniter函数中设置文件上传..

但无论我尝试做什么,我都会收到错误......

任何人都可以帮我解决问题吗?

我在控制器中的功能。

function insert_user_details(){

            $this->load->library('form_validation');
            $this->form_validation->set_rules('UserName', 'Username', 'callback_username_check');
            //$this->form_validation->set_rules('Password', 'Password', 'required');
            //$this->form_validation->set_rules('Email', 'Email', 'required|email');
            //$this->form_validation->set_rules('FirstName','FirstName', 'required');
            //$this->form_validation->set_rules('MiddleName','FirstName', 'required');
            //$this->form_validation->set_rules('LastName','FirstName', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                echo 'Please Fill in the Forms Correctly' ;
            }
            else
            {
                $config['upload_path'] = '../user_uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $this->load->library('upload', $config);
                $pass = $this->input->post('Password');
                $pass = md5($pass);
                $data = array(
                    'UserName'=>$this->input->post('UserName'),
                    'Password'=>$pass,
                    'FirstName'=>$this->input->post('FirstName'),
                    'MiddleNames'=>$this->input->post('MiddleName'),
                    'LastName'=>$this->input->post('LastName'),
                    'DateOfBirth'=>date('Y-m-d',strtotime($_POST['DateOfBirth'])),
                    'AddressLine1'=>$this->input->post('AddressLine1'),
                    'AddressLine2'=>$this->input->post('AddressLine2'),
                    'City'=>$this->input->post('City'),
                    'CountryID'=>$this->input->post('CountryID'),
                    'NationalityCountryID'=>$this->input->post('NationalityCountryID'),
                    'MobileNo'=>$this->input->post('MobileNo'),
                    'WorkPhoneNo'=>$this->input->post('WorkPhoneNo'),
                    'WorkPhoneExtention'=>$this->input->post('WorkPhoneExtension'),
                    'NationalTaxNumber'=>$this->input->post('NationalTaxNumber'),
                    'NationalIDCardNo'=>$this->input->post('NationalIDCardNo'),
                    'MaritalStausID'=>$this->input->post('UI_LanguageID'),
                    'ReligionID'=>$this->input->post('ReligionID'),
                    'Photograph' => $this->upload->data(),
                    'Email'=>$this->input->post('Email'),
                    'WebUrl'=>$this->input->post('WebUrl'),
                    'HighestEducation'=>$this->input->post('HighestEducation'),
                    'UserResumeFile'=> 'res.jpg',
                    'UI_LanguageID'=> $this->input->post('UI_LanguageID'),
                    'LastLoginDate'=>'2013-02-24 00:00:00',
                    'LastLoginIP'=>'192.168.1.2',
                    'Remarks'=>$this->input->post('Remarks'),
                    'CreateByUserID'=>'1',
                    'CreateAt'=>'2013-02-24 00:00:00',
                    'ModifiedByUserID'=>'2',
                    'ModifiedAt'=>'2013-02-24 00:00:00',
                    'IsActive'=>'1'
                );
                $table = 'sys_user_accounts';
                $this->common_model->insert_record($table, $data);
                echo 'Success Message' ;
                //$this->load->view('user_management/success','','true');
        }
    }

这是视图文件。

 '照片',         'id'=> '照片',         'placeholder'=> '上传用户照片',         'maxlength'=> '250'     );     echo form_upload($ input_field_attributes);     ?>

是的,我使用了多部分

echo form_open_multipart('user_management/manage_users',array('id' => 'insert_user'));

所以有人能告诉我为什么我在firebug中得到以下错误?以及如何解决它?

> Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `sys_user_accounts` (`UserName`, `Password`, `FirstName`, `MiddleNames`, `LastName`, `DateOfBirth`, `AddressLine1`, `AddressLine2`, `City`, `CountryID`, `NationalityCountryID`, `MobileNo`, `WorkPhoneNo`, `WorkPhoneExtention`, `NationalTaxNumber`, `NationalIDCardNo`, `MaritalStausID`, `ReligionID`, `Photograph`, `Email`, `WebUrl`, `HighestEducation`, `UserResumeFile`, `UI_LanguageID`, `LastLoginDate`, `LastLoginIP`, `Remarks`, `CreateByUserID`, `CreateAt`, `ModifiedByUserID`, `ModifiedAt`, `IsActive`) VALUES ('hello', '1a1dc91c907325c69271ddf0c944bc72', 'fda', 'fda', 'fad', '2010-03-03', '1', 0, '1', 0, 0, '1', '1', 0, '1', 0, 0, 0, Array, 0, 0, 0, 'res.jpg', 0, '2013-02-24 00:00:00', '192.168.1.2', 0, '1', '2013-02-24 00:00:00', '2', '2013-02-24 00:00:00', '1')

Filename: C:\xampp\htdocs\projects\zorkif_nextgen\system\database\DB_driver.php

Line Number: 330

4 个答案:

答案 0 :(得分:1)

我对CI并不是很熟悉,但我可以看到$this->upload->data();正在返回一个数组,这就是为什么它会抛出错误。

如果您使用的是PHP 5.4,请尝试使用$this->upload->data()['file_name']$this->upload->data()['full_path'],如果您使用的是PHP 5.3或更低版本,则只需将$this->upload->data()分配给某个变量,而不是通过正确的密钥访问值

在插入之前执行此操作var_dump($this->upload->data());,我相信你会明白这一点。

答案 1 :(得分:0)

在你的$ data数组,'Photograph'索引中,你要分配$ this-> upload-> data()。实际上$ this-> upload-> data()这个辅助函数返回一个数组。详情here

如果您要保存照片的名称,请尝试使用

$name = $this->upload->data();

然后在你的数组中

'Photograph' => $name['file_name']

答案 2 :(得分:0)

尝试调试$this->upload->do_upload('image_file_name') //您的实际输入字段名称

在上传之前请检查条件     //检查上传是否成功

if(!$this->upload->do_upload('image_file_name'))
{
    // If all uploading condition not mate like image size/type etc.
}

答案 3 :(得分:0)

'Photograph' => $this->upload->data()

$ this-> upload-> data()是一个返回数组的CI函数,因此要将其插入到DB的列中,您需要将其序列化,将其更改为字符串或者只是设置“照片”字段数组中的一个值。