【问题标题】:want to change image in codeigniter想在codeigniter中更改图像
【发布时间】:2017-05-29 22:11:38
【问题描述】:

我想在 Codeigniter 中更改一张图片,替换之前上传的图片并从文件夹中删除之前的图片:

这是我的控制器。但它不起作用:

public function changeImage(){

    //$user_id = $this->profile_model->changeImage();

        /************* File Upload ************/
        $config['upload_path'] = './uploads/user_pic/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $this->load->library('upload',$config);

        $filetype = $_FILES['user_pic']['type'];
        $file_name = $_FILES['user_pic']['name'];

        if($filetype == "image/jpg")
                $file_type='jpg';
            else if ($filetype == "image/gif")
                $file_type='gif';
            else if($filetype == "image/jpeg")
                $file_type='jpg';

            else if($filetype == "image/pjpeg")
                $file_type='pjpeg';
            else if($filetype ==  "image/png")
                $file_type='png';

        $_FILES['user_pic']['name']=$user_id.'.'.$file_type;

        $this->upload->do_upload('user_pic');


        $up_dtat = array('user_pic' => $_FILES['user_pic']['name']);
        $this->db->where('user_id',$user_id);
        $this->db->update('tbl_users',$up_dtat);
    redirect('profile');
}

【问题讨论】:

    标签: image codeigniter upload


    【解决方案1】:

    如果您需要更新商店文件中的头像,首先您需要删除旧文件,根据用户 ID 获取旧文件名。

    然后用这个代码给出文件名。

    unlink("your_path/filename.jpg"); //don't use base_url() just give the path like profile_pic/xxxxx.png
    

    此代码将从该文件夹中删除指定的文件。

    现在我有用于文件上传的代码,所以它也适用于你试试这个。

    $imgpath='uploads/profile_pic/';//path where do you want to store image
                $all_img="";
                if($_FILES["profile_pic"]['name'])//input type that you have use in file upload
                {         
                   $path_parts = pathinfo($_FILES["profile_pic"]["name"]);
                   $image_path = $path_parts['filename'].'_'.time().'.'.$path_parts['extension']; 
                   $all_img.=$image_path;
                   move_uploaded_file($file_tmp=$_FILES["profile_pic"]["tmp_name"],$imgpath."/".$image_path);
                   $data['cm_user_profile_pic']=$all_img;//store filename in array to update in database
                }
    

    【讨论】:

      【解决方案2】:

      如果你只想替换之前的图像,但设置文件名相同,那么

      第一种方法是:

      从表中获取旧文件名

      $sql = "SELECT user_pic FROM tbl_users WHERE user_id=?";
      $get_image = $this->db->query($sql,array($user_id))->row_array();
      

      将新文件名设置为旧文件名

      $config['file_name'] = $get_image['user_pic'];
      $config['overwrite'] = TRUE; // this will overwrite your image
      $this->load->library('upload', $config);
      

      但在这种情况下,旧文件和新文件扩展名必须相同 如果两个扩展名不同,那么上传后图片可能会崩溃

      第二种方法是

      从数据库中获取 user_pic 数据

      然后取消链接该图片

      unlink($_SERVER['DOCUMENT_ROOT'].'/'.Your_image_folder.$get_image['user_pic']);
      

      检查文件是否上传

       if ( ! $this->upload->do_upload('user_pic')) {
                  $error = array('error' => $this->upload->display_errors()); 
               }
      
               else { 
                  $data = array('upload_data' => $this->upload->data()); 
               } 
      

      【讨论】:

      • 不工作...请检查我在最后评论@Nidhi 的编辑功能
      • 向我解释问题@Sabbir... 不工作但在哪种情况下?图像未被覆盖或未被删除??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      相关资源
      最近更新 更多