【问题标题】:how to do image upload in codeigniter?如何在codeigniter中上传图片?
【发布时间】:2017-09-16 11:02:06
【问题描述】:

我正在尝试上传根文件夹中的图像及其在数据库中的文件名。这是我为上传功能所做的:

       public function add_blog($id=0){
       if(!empty($_FILES['picture']['name'])){
             $config['upload_path'] = 'uploads/blog_image';
             $config['allowed_types'] = 'jpg|jpeg|png|gif';
             $config['file_name'] = $_FILES['picture']['name'];

             //Load upload library and initialize configuration
             $this->load->library('upload',$config);
             $this->upload->initialize($config);
             // print_r($value['name'][$s]);exit;
             if($this->upload->do_upload('picture')){
                 $uploadData = $this->upload->data();
                 $picture = $uploadData['file_name'];
                  print_r($picture); exit;
             }
           }
             print_r($config['file_name']); exit;


     $data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
                          'posted_by'=>$this->input->post('posted_by'),
                           'blog_image'=>$picture);

 if ($id==0){
      $this->db->insert('blog',$data['blog_data']);
        // $last_id = $this->db->insert_id();

      }
    else {
     $this->db->where('id',$id);
    //  $last_id = $this->db->insert_id();
     $this->db->update('blog',$data['blog_data']);

 }
 }

这里的问题是我能够插入除图像之外的其他数据。如果我执行 print_r() 并退出,我会得到带有 print_r($config[file_name]) 的图像名称,否则它只会插入除图像之外的其他数据。但是图像既没有上传到根文件夹,也没有上传到数据库中。如果我给出不存在的上传路径,那么它也不会引发任何错误。我认为 If 里面的代码没有被执行。我该如何解决这个问题?提前致谢。

【问题讨论】:

    标签: codeigniter image-uploading


    【解决方案1】:
    private function _upload_image(  ) {
        $this->load->library( 'upload' );
        if ($_FILES && $_FILES['picture']['name'] !== ""){
            $config['upload_path']   = 'uploads/blog_image';
            $config['allowed_types'] = 'jpg|jpeg|png|bmp';
            $config['max_size']      = 10000;
            /*the picture name must be unique, use function now()*/
            $config['file_name']     = $_FILES['picture']['name'] . now();
            $config['file_ext_tolower']     = TRUE;
            $this->upload->initialize( $config );
            if ( $this->upload->do_upload( 'picture' ) ){
                $file_name = $this->upload->data()['file_name'];
                $full_path = $this->upload->data()['full_path'];
                /*If you want create a thumb, use this part*/
                $this->load->library('image_lib');
                $config = array(
                    'source_image'      => $path,
                    'new_image'         => $this->_image_path,
                    'maintain_ratio'    => true,
                    'width'             => 128,
                    'height'            => 128,
                    'create_thumb'      => TRUE,
                    'thumb_marker'      => '_thumb',
                );
                $this->image_lib->initialize( $config );
                $this->image_lib->resize();
                /*Save in database*/
                $this->db->insert('blog', [
                     'file_name' => $file_name,
                     'full_path' => $full_path
                ]);
            } else {
                //if picture is empty, do something
            }
        }
    }
    

    您不需要使用 $_FILES && $_FILES ['picture']['name']! == "" 仅当您的表单具有图片字段作为可选字段时,$this->upload->do_upload('picture') 并从 $this->upload->data() 获取数据,请阅读手册

    public function add_blog()
        {
                $config['upload_path']          = '.uploads/blog_image';
                $config['allowed_types']        = 'jpg|jpeg|png|gif';
                $config['max_size']             = 10000;
    
                $this->load->library('upload', $config);
    
                if ( ! $this->upload->do_upload('picture'))
                {//Do something with errors
                        $errors = $this->upload->display_errors();
                }
                else
                {
                        $data = $this->upload->data();
                        $this->db->insert('blog', [
                               'file_name' => $data['file_name'],
                               'full_path' => $data['full_path']
                       ]);
                }
        }
    

    【讨论】:

    • 我们不能用一点简单的方式来做吗?
    【解决方案2】:

    我只是没有提到要上传的文件大小。我在上面的代码中做了这个并且工作了。 编辑

        public function add_blog($id=0){
           if(!empty($_FILES['picture']['name'])){
                 $config['upload_path'] = 'uploads/blog_image';
                 $config['allowed_types'] = 'jpg|jpeg|png|gif';
                 $config['max_size']             = 0;
                 $config['file_name'] = $_FILES['picture']['name'];
    
                 //Load upload library and initialize configuration
                 $this->load->library('upload',$config);
                 $this->upload->initialize($config);
                 // print_r($value['name'][$s]);exit;
                 if($this->upload->do_upload('picture')){
                     $uploadData = $this->upload->data();
                     $picture = $uploadData['file_name'];
                      // print_r($picture); exit;
                 }
               }
                //  print_r($config['file_name']); exit;
    
    
     $data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
                              'posted_by'=>$this->input->post('posted_by'),
                               'blog_image'=>$picture);
    
     if ($id==0){
          $this->db->insert('blog',$data['blog_data']);
            // $last_id = $this->db->insert_id();
    
     }
     else {
         $this->db->where('id',$id);
        //  $last_id = $this->db->insert_id();
         $this->db->update('blog',$data['blog_data']);
    
     }
     }
    

    此代码适用于插入和更新。

    【讨论】:

      猜你喜欢
      • 2013-06-23
      • 2022-12-07
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      相关资源
      最近更新 更多