图像路径没有保存在Codeigniter的数据库中?

时间:2015-08-05 17:22:22

标签: php codeigniter

我正在尝试将文件路径放到数据库中,我已经上传了文件,但我不知道如何获取文件的路径将其放入数据库中?我已经搜索过SOF了!!!

Update_profile控制器

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

ini_set("display_errors",1);

class Update_profile extends CI_Controller {


     function __construct()
     {
          // Call the Model constructor
          parent::__construct();
          $this->load->library('session');
          $this->is_login();
          $this->load->helper(array('form', 'url'));
        //  $this->load->model('Edit_profile'); 
          $this->load->model('Insert_article');      
     }


    public  function index() {

    //  $this->load->view('header2');
        $this->load->view('edit_profile');

    }// index function ends

    public function is_login() {

      $is_login=$this->session->userdata('is_login');
      if(!isset($is_login) || $is_login !=true)
      {
        //don't echo the message from controller 
        echo "you don't have permission to access this page <a href=../Homecontroller/index/>Login</a>";
        die();
      }
  } //is_login function ends

  // function to upload images
    function upload()
    {
        $id = $this->session->userdata('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'] = './upload/large/';
        $config['allowed_types'] = 'jpg|png|gif';
        $config['max_size']    = '10248';

        //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($upload_data);
            // call to model function *********
            $data['images'] = $this->Edit_profile->upload_image();

            $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);

        }
    }


} //class ends   
?>

我的模型更新个人资料

<?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)
        {
            $query_insert = "INSERT INTO tbl_usrs (profile_picture) VALUES (?)";
            $this->db->query($query_insert);

        }


}

我有35个字段的tbl_usrs和profile_picture用于上传个人资料照片?

1 个答案:

答案 0 :(得分:0)

http://www.codeigniter.com/userguide3/libraries/file_uploading.html

添加常量文件

define('LARGEPATH','./upload/large/');
define('THUMBPATH','./upload/thumb/');

控制器

    $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']    = '10248';



 $upload_data = $this->upload->data();
 $data['images'] = $this->Edit_profile->upload_image($upload_data);
 $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);

Edit_profile模型

   public function upload_image($data)  
  {  
      $filePath = ltrim(LARGEPATH.$data['file_name'],'.');
       $query_insert = "INSERT INTO tbl_usrs (profile_picture) VALUES (".$filepath.")";
        $this->db->query($query_insert);

  } 

如果要调整图像大小 http://www.codeigniter.com/userguide3/libraries/image_lib.html