dsfykqq3403 2011-10-22 22:25
浏览 36
已采纳

Codeigniter:如何将文件路径上传到数据库?

I am trying to put the file path to database, I already uploaded the file but I don't know how to get the path of the file to put it in the database?

Here is the controller:

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

class dogo extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('insert_article');
    }   
    public function index()
    {

    $this->load->view('dogo/dashboard.php');

    }
    //add new post to database including uploading image
    public function new_post()
    {

    //used for the dropdown menu (category)
    $this->load->model('dogo_cat');
    $data['categores_dropdown'] = $this->dogo_cat->get_categories();    

    //form validation
    $this->load->library('form_validation');//load the validation library

    $this->form_validation->set_rules('title', 'title of the post', 'required');
    $this->form_validation->set_rules('text', 'text of the post', 'required');
    //$this->form_validation->set_rules('image', 'image of the post', 'required');
    $this->form_validation->set_rules('category', 'category of the post', 'required');  

    //the form validation condition
    if($this->form_validation->run() == FALSE){
        //error
        //$this->view_data
        $this->load->view('dogo/new_post.php', $data);      
    }else{

        //no error in the form
        $title = $this->input->post('title');
        $text = $this->input->post('text');
        $category = $this->input->post('category');
        $publish = $this->input->post('publish');
        //$img_nw = $this->input->post('img_nw');
        //$img_nw = $this->input->post('img_nw');
        $image_file = $this->input->post('image_file');



        //uploading
        $this->load->model('upload_new_post');
        if($this->input->post('upload')){
        $this->upload_new_post->do_upload();

        //$this->insert_article->insert_new_post($title, $category, $img_nw, $text, $source, $publish);

        $data['images'] = $this->upload_new_post->get_images();

        echo "title of the post: " . $title . "<br /> and the text of the post " . $text . "<br /> and category is: " . $category . "<br /> and publish is: " .$publish . "<br /> and image: <pre>" . $do_upload ."</pre>";




            //echo $img_nw;


            $this->load->view('dogo/new_post.php', $data);              


        }



    }






    }   
}

And here is the model to upload it:

    <?php
class upload_new_post extends CI_Model{
    // retreive categories 
    var $file_path;
    var $file_path_url;

        function __construct() {
        parent::__construct();
            $this->file_path = realpath(APPPATH . '../post_data/images');
            $this->file_path_url = base_url().'post_data/images/';
    }

    function do_upload(){
        $config=array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->file_path,
        'max_size' => 2000 
        );


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

        $image_data = $this->upload->data();

        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->file_path . '/thumbs',
            'maintain_ration' => true,
            'width' => 150,
            'height' => 150
        );

        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
    }

    function get_images(){
        $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;
    }

}       

And here the model to insert query:

    <?php
class Insert_article extends CI_Model{

    //insert new post
    function __construct() {
        parent::__construct();
    }   

    function insert_new_post($title, $category, $img_nw, $text, $source, $publish)
    {
        $query_insert = "INSERT INTO hs_news_nw (idcat_nw, idsc_nw, idusr_nw, title_nw, img_nw, text_nw, active_nw, totalview_nw, date_nw) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        $this->db->query($query_insert, array($category, $source, 1, $title, $img_nw, $text, 1, 1000, '2011-10-12 02:01:24'));
    }
}       
  • 写回答

4条回答 默认 最新

  • doushi3202 2011-10-23 05:32
    关注

    You should return $image_data from do_upload() function in your upload_new_post model.

    $image_data = $this->upload->data();
    ..
    ..
    return $image_data;
    

    $image_data contains all the info about uploaded file including file name and path (do a print_r). Then you can pass it onto Insert_article model from your controller to store in database.

    For reference:

    http://codeigniter.com/user_guide/libraries/file_uploading.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的