我正在尝试将文件路径放到数据库中,我已经上传了文件,但我不知道如何获取文件的路径将其放入数据库中?
这是控制器:
<?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, $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'));
}
}
答案 0 :(得分:1)
您应该从$image_data
模型中的do_upload()
函数返回upload_new_post
。
$image_data = $this->upload->data();
..
..
return $image_data;
$image_data
包含有关上传文件的所有信息,包括文件名和路径(执行print_r)。然后,您可以将其从控制器传递到Insert_article
模型,以存储在数据库中。
供参考:
http://codeigniter.com/user_guide/libraries/file_uploading.html
答案 1 :(得分:1)
请尝试$fpath
为您提供正确的路径:
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|txt|php|pdf';
$config['max_size'] = '9000';
$config['encrypt_name'] = true;
$image_data = $this->upload->data();
$fname=$image_data[file_name];
$fpath=$image_data[file_path].$fname;
答案 2 :(得分:0)
如果我正确理解你,你可以使用realpath来获取真实的文件位置
答案 3 :(得分:0)
您可以尝试通过文件处理 图书馆来上传..
控制器:
public function add_slide() {
// set the page name
$data['title_admin'] = "Add & View Slide Information";
// load the custom file processing library
$this->load->library('file_processing');
$this->load->library('form_validation');
// check if click on the submit button
if ($this->input->post('Save')) {
// write the validation rule
$this->form_validation->set_rules('slide_name', 'Name', 'required');
$this->form_validation->set_rules('slide_short_description', 'Short Description', '');
$this->form_validation->set_rules('slide_long_description', 'Detail Description', '');
$this->form_validation->set_rules('slide_image', 'Image', 'callback_file_validate[no.slide_image.jpg,gif,png]');
//$this->form_validation->set_rules('slide_mission_vision', 'Diabetes Profile', 'callback_file_validate[no.slide_mission_vision.pdf]');
// check the validation
if ($this->form_validation->run()) {
$addData['slide_name'] = $this->input->post('slide_name');
$addData['slide_short_description'] = $this->input->post('slide_short_description');
$addData['slide_long_description'] = $this->input->post('slide_long_description');
$addData['slide_image'] = $this->file_processing->image_upload('slide_image', './images/slide/', 'size[500,1000|100,500]');
//$addData['slide_mission_vision'] = $this->file_processing->file_upload('slide_mission_vision', './images/slide_mission_vision/', 'pdf');
$addData['slide_add_date'] = date('Y-m-d');
// call the crate model and inset into database
if ($this->sa_model->save_slide_info($addData)) {
$this->session->set_flashdata('success_msg', 'Save Information Successfully!!');
redirect('super_admin/add_slide');
}
else
$data['error_msg'] = mysql_error();
}
}
// load the views
$data['s2'] = TRUE;
$data['slide_info'] = $this->sa_model->select_all_slide_info();
$data['admin_main_content'] = $this->load->view('admin/add_slide', $data, true);
$this->load->view('admin/admin_master', $data);
}
// file validation
public function file_slide_validate($fieldValue, $params) {
// get the parameter as variable
list($require, $fieldName, $type) = explode('.', $params);
// get the file field name
$filename = $_FILES[$fieldName]['name'];
if ($filename == '' && $require == 'yes') {
$this->form_validation->set_message('file_validate', 'The %s field is required');
return FALSE;
} elseif ($type != '' && $filename != '') {
// get the extention
$ext = strtolower(substr(strrchr($filename, '.'), 1));
// get the type as array
$types = explode(',', $type);
if (!in_array($ext, $types)) {
$this->form_validation->set_message('file_validate', 'The %s field must be ' . implode(' OR ', $types) . ' !!');
return FALSE;
}
}
else
return TRUE;
}
型号:
// insert new record into user table
public function save_slide_info($data) {
$this->db->insert('tbl_slide', $data);
return $this->db->affected_rows();
}