Member Avatar for anmol.raghuvanshi1
anmol.raghuvanshi1

hello,
I want to store image path in database along with image name like /uploads/profilepic/name.jpg.
what happening is only path is beinged saved not image name like field in my table prfile_picture has updated /uploads/profilepic/
but i want image name also after trailing slash so that i can retrieve that image by name
plz help me out this.....

//controller

function upload()
    {
        $session_data = $this->session->userdata('sessiondata');
        $user_id = $session_data['user_id'];
        //post image
        $img=$this->input->post("filename");
        //set preferences
        $config['remove_spaces']=TRUE;
        $config['encrypt_name'] = TRUE; // for encrypting the name
        $config['upload_path'] = LARGEPATH;
        $config['allowed_types'] = 'jpg|png|gif';
        $config['max_size']    = '100000000';

        //load moadel ********
        $this->load->model('Edit_profile');
        //load upload class library
        $this->load->library('upload', $config);

        //$this->upload->do_upload('filename') will upload selected file to destiny folder
        if (!$this->upload->do_upload('filename'))
        {
            // case - failure
            $upload_error = array('error' => $this->upload->display_errors());
            $this->load->view('edit_profile', $upload_error);
        }
        else
        {
            // case - success
            //callback  returns an array of data related to the uploaded file like the file name, path, size etc
            $upload_data = $this->upload->data();
            //return $upload_data;
            print_r($this->upload->data());
            // call to model function *********
            //$data['images'] = $this->Edit_profile->upload_image();
            $data['images'] = $this->Edit_profile->insert_new_post($img);

            $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
            $this->load->view('edit_profile', $data);

        }
    }

//model to insert

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

ini_set("display_errors",1);

class Edit_profile extends CI_Model {

      var $file_path;
      var $file_path_url;

     function __construct()
     {
          // Call the Model constructor
          parent::__construct();
          $this->file_path = realpath(APPPATH . '../upload/large');
          $this->file_path_url = base_url().'upload/large/';
          //$this->is_login();
          //$this->load->helper(array('form', 'url'));


     }

        public function upload_image()  
      {  
        $files = scandir($this->file_path);
        $files = array_diff($files, array('.', '..', 'thumbs'));
        $images = array();
        foreach ($files as $file){
        $images [] = array(
          'url' => $this->file_path_url . $file,
          'thumb_url' => $this->file_path_url . 'thumbs/' .$file
         );

        }
        return $images;


      }
         function insert_new_post($img)
    {

        $session_data = $this->session->userdata('sessiondata');
        $user_id = $session_data['user_id'];

        $filePath = ltrim(LARGEPATH.$img['file_name'],'.');
        print_r(LARGEPATH);
        $query = "UPDATE `tbl_usrs` set profile_picture='".$filePath."' where user_id='".$user_id."'";
       // $this->db->query($query,array($img));
       $arg=array ($img);

        if($this->db->query($query,$arg)==true)
            {
                return true; // if added to database
            }else {
                    return false;
            }

    }

}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.