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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Codeigniter:如何将文件路径上传到数据库?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图把文件路径放到数据库,我已经上传文件,但我不知道如何获取文件的路径将其放入数据库

这是控制器:

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


        }



    }






    }   
}

这是上传它的模型:

<?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;
    }

}

这里插入查询的模型:

<?PHP
class Insert_article extends CI_Model{

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

    function insert_new_post($title,$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,1,$title,1000,'2011-10-12 02:01:24'));
    }
}

解决方法

应该在upload_new_post模型中从do_upload()函数返回$image_data.

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

$image_data包含有关上传文件的所有信息,包括文件名和路径(执行print_r).然后,您可以将其从控制器传递到Insert_article模型以存储在数据库中.

以供参考:

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – Codeigniter:如何将文件路径上传到数据库?全部内容,希望文章能够帮你解决php – Codeigniter:如何将文件路径上传到数据库?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。