简体   繁体   中英

How to save an image in CodeIgniter root folder and database?

I need to make the user able to change the profile image, but I can't find clear documentation showing how the CodeIgniter upload process works by teaching the walkthrough.

I already created a column called user_profile within the main table.

I the controller I define the size , type and make the encryption of it.

public function update_profile(){

    $config["upload_path"] = "public/images/profile";
    $config["allowed_types"] = "jpg|jpeg|gif|png";
    $config["max_size"] = 700;
    $config["max_width"] = 1024;
    $config["max_height"] = 768;
    $config["encrypt_name"] = TRUE;

    $this->load->library("upload", $config);

    if($this->upload->do_upload('user_profile'))
    {
        $info_arquivo = $this->upload->data();
        $user_profile = $info_arquivo["file_name"];

        $this->load->model("profile_model");

        $image_perfil = array(
            "user_profile" => $user_profile
        );

        $query = $this->profile_model->insert_image($image_perfil);

        if($query){
            $this->session->set_flashdata('msg', 'Imagem de perfilfoi atualizada');
            redirect(base_url('admin/dashboard'), 'refresh');
        } else {
            $this->session->set_flashdata('msg', 'Imagem de perfil não foi atualizada');
            redirect(base_url('admin/profile'), 'refresh');
        }
    }
    else
    {
        $this->session->set_flashdata('msg', 'Imagem de perfil não foi atualizada');
        redirect(base_url('admin/profile'), 'refresh');
    }

}

And in the admin / profile view I put the following form.

                <!-- UPLOAD IMAGE USER -->
                <?php echo form_open_multipart('admin/update_profile'); ?>
                    <div    class="form-group">
                        <label>Selecione uma imagem</label>
                        <input type="file" name="user_profile" class="form-control" id="user_profile" required/>
                    </div>
                    <div    class="form-group">
                        <button type="submit" class="btn btn-success pull-right" value="cadastrar">Cadastrar</button>
                    </div>
                </form>
                <!-- END UPLOAD IMAGE USER -->

And in my Profile_model model I insert the filename into the user_profile column

class Profile_model extends CI_Model{

    public function insert_image($user_profile)
    {   
        $this->db->insert('ci_users', $user_profile);

        return $this->db->affected_rows() ? TRUE : FALSE;
    }

}

I think your Error on form URL. please Add index.php on below HTML form

   <!-- UPLOAD IMAGE USER -->
            **<?php echo form_open_multipart('index.php/admin/update_profile'); ?>**
                <div    class="form-group">
  public function update_profile()
  {
    $config['upload_path']   = 'public/images/profile/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size']      = 1000; 
    $config['max_width']     = 1024; 
    $config['max_height']    = 768;  
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('user_profile')) {
        $error = array('error' => $this->upload->display_errors()); 
        echo "<pre>";
        print_r($error);
        echo "</pre>";
        die();
        $this->session->set_flashdata('msg', 'Imagem de perfil não foi atualizada');
        redirect(base_url('admin/profile'), 'refresh');
        //$this->load->view('upload_form', $error); 
    }

    else { 
        $info_arquivo = $this->upload->data(); 
        $user_profile = $info_arquivo["file_name"];

        $this->load->model("profile_model");

        $image_perfil = array(
            "user_profile" => $user_profile
        );

        $query = $this->profile_model->insert_image($image_perfil);

        if($query){
            $this->session->set_flashdata('msg', 'Imagem de perfilfoi atualizada');
            redirect(base_url('admin/dashboard'), 'refresh');
        } else {
            $this->session->set_flashdata('msg', 'Imagem de perfil não foi atualizada');
            redirect(base_url('admin/profile'), 'refresh');
        }
    } 
}

VIEW

 <!DOCTYPE html>
 <html>
 <head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  </head>
  <body>

   <form action="<?php echo base_url('stackoverflow/update_profile');?>" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label>Selecione uma imagem</label>
        <input type="file" name="user_profile" class="form-control" id="user_profile" required/>
    </div>
    <div    class="form-group">
        <button type="submit" class="btn btn-success pull-right" value="cadastrar">Cadastrar</button>
    </div>
</form>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM