Codeigniter:如何获取文件的文件名 [英] Codeigniter: how to get a filename of a file

查看:127
本文介绍了Codeigniter:如何获取文件的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Codeigniter菜鸟,我试图获取上传的图像的文件名,以便我可以将其保存在数据库中。
我有两个模型,homemodel处理我的数据库和image_upload_model处理图像上传。
一切正常,除非我不知道如何将图像文件名发布到数据库

i am a Codeigniter rookie and i am trying to get the file name of an uploaded image so that i can save it in the database. i have two models, homemodel deals with my database and the image_upload_model deals with image uploading. Everything works fine except i dont know how to post the image filename to the database

image_upload_model.php b
$ b

image_upload_model.php

<?php

class Image_upload_model extends CI_Model {

    var $image_path;

    //constructor containing the image path
    function Image_upload_model() {

        $this->image_path = realpath(APPPATH.'../assets/img');
    }

    //uploading the file
    function do_upload() {

        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->image_path
        );
        $this->load->library('upload',$config);
        $this->upload->do_upload();
    }
}
?>

homemodel.php

<?php

class homeModel extends CI_Model {

    //inserting into the table tenants
    function addTenants() {

        $this->load->model('Image_upload_model');

        $data = array(
            'Fname' => $this->input->post('Fname'),
            'Lname' => $this->input->post('Lname'),
            'contact' => $this->input->post('contact'),
            'email' => $this->input->post('email'),
            'location' => $this->input->post('location'),
            'img_url' => " "//the filename of the image should go here
        );

        $this->db->insert('tenants', $data);
    }
}
?>

控制器

homecontroller.php / p>

The controller
homecontroller.php

<?php

class HomeController extends CI_Controller {

    public function index() {

        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('homemodel');
        $this->load->model('Image_upload_model');

        if ($this->input->post('submit') == 'Submit') {

            $this->homemodel->addTenants();
            echo 'inserted';
            $this->Image_upload_model->do_upload();
            echo 'image uploaded';
        }
        $this->load->view('index.php');
    }
}
?>

任何帮助,谢谢!

推荐答案

您可以像下面这样获得文件名:

You can get file name like this

  $upload_data = $this->upload->data(); 
  $file_name =   $upload_data['file_name'];

这篇关于Codeigniter:如何获取文件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆