【问题标题】:codeigniter upload image with other datacodeigniter 上传图片和其他数据
【发布时间】:2012-09-10 05:36:16
【问题描述】:

我正在提交一个表单,用户可以上传该表单中的图片,然后提交整个表单。我在 codeigniter 网站上找到了一个显示上传表单的教程(仅用于上传,而不是其他数据)。链接是:Codeigniter Upload Tutorial。如何提交表单,然后上传文件,同时将其他详细信息上传到数据库中的其他表?

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    下面是一个例子

        function add_staff(){
                $this->load->library('form_validation');
                $this->load->helper(array('form', 'url'));
                // field name, error message, validation rules
                $this->form_validation->set_rules('name', 'Full name', 'trim|required');
                                $this->form_validation->set_rules('designation', 'Last Name', 'trim|required');
                if($this->form_validation->run() == FALSE)
                {   
                    $data['main_content']   =   'staff/add_staff';
                    $this->load->view('admin/template',$data);
                }
                else
                {
                    $config['upload_path'] = 'uploads/staff/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    
    
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);
                    if ( ! $this->upload->do_upload('file'))
                    {
                                                $data['error'] = array('error' => $this->upload->display_errors());
    
                        $new_staff = array(
                                'name' => $this->input->post('name'),
                                'designation' => $this->input->post('designation'),
                                                                'phone' => $this->input->post('phone'),
                                                                'email' => $this->input->post('email'),
                                                                'address' => $this->input->post('address'),
                                                                'description' => $this->input->post('description'),
                                'status' => $this->input->post('status')
                        );
    
                    }
                    else
                    {   
                        $file_data  =   $this->upload->data('file');
    
                        $new_staff = array(
                                'name' => $this->input->post('name'),
                                'designation' => $this->input->post('designation'),
                                                                'phone' => $this->input->post('phone'),
                                                                'email' => $this->input->post('email'),
                                                                'address' => $this->input->post('address'),
                                                                'photo' => $file_data['file_name'],
                                                                'description' => $this->input->post('description'),
                                'status' => $this->input->post('status')
                        );
    
                    }                   
                        if($this->staff_model->add_staff($new_staff)):
                            $this->session->set_flashdata('success', 'Staff added Sucessfully !!');
                        endif;
                        redirect('admin/staff');                        
                }
    
        }
    

    【讨论】:

    • 对不起 sujeet,但是这段代码的目的是什么?目前还不清楚它的用途是什么(我也是 CI 新手)
    • (1) 如果不是 isset($_POST) 则重定向到表单 (2) 如果 isset($_POST) 并且满足验证,则上传文件并调用模型以插入数据库。适用于添加和编辑带有图像的表单。如果在编辑时提供了图像,则替换图像。如果不使用当前图像
    【解决方案2】:

    您也可以将其他数据连同表单文件一起传递给控制器​​,例如

    <?php echo form_open_multipart('upload/do_upload');?>
        <input type="text" name="someField" value=""  />
        <input type="file" name="userfile" size="20" />
        <input type="submit" value="upload" />
    </form>
    

    在您的 upload 控制器的 do_upload 函数中:

    $someField = $this->input->post("somefield"); //save to some db
    //and rest of your file to be uploaded code from the same link you provided
    

    你的意思是这样的

    【讨论】:

    • 这行得通吗?因为有一个关于上传文件的完整示例,我可以在其中检查图像类型和所有内容。那么我应该将 $somefield 传递给该方法吗?
    【解决方案3】:

    Image MOO 是一个出色的库,可以即时完成图像处理....

    例如,要将上传的图片调整为特定路径,您只需这样做

    public function thumbnailer($uploader_response,$field_info,$files_to_upload)
    {
        $this->load->library('image_moo');
    $file_uploaded=$field_info->upload_path.'/'.$uploader_response[0]->name;
    $thumbnails=$field_info->upload_path.'/thumbnails/'.$uploader_response[0]->name;
        $cart_thumbnails=$field_info->upload_path.'/cart_thumbnails/'.$uploader_response[0]->name;
    $this->image_moo->load($file_uploaded)->resize(400,250)->save($thumbnails,false);
    }
    

    希望对你有帮助!!

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2013-08-23
      • 2011-06-08
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多