php – Codeigniter:如何将文件路径上传到数据库?
发布时间:2020-12-13 16:13:48 所属栏目:PHP教程 来源:网络整理
导读:我试图把文件路径放到数据库,我已经上传了文件,但我不知道如何获取文件的路径将其放入数据库? 这是控制器: ?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');class dogo extends CI_Controller { function __construct() { pare
我试图把文件路径放到数据库,我已经上传了文件,但我不知道如何获取文件的路径将其放入数据库?
这是控制器: <?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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |