加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Codeigniter:如何获取文件的文件名

发布时间:2020-12-13 13:05:49 所属栏目:PHP教程 来源:网络整理
导读:我是Codeigniter新手,我试图获取上传图像的文件名,以便我可以将其保存在数据库中. 我有两个模型,homemodel处理我的数据库,image_upload_model处理图像上传. 一切正常,但我不知道如何将图像文件名发布到数据库 image_upload_model.php ?phpclass Image_upload
我是Codeigniter新手,我试图获取上传图像的文件名,以便我可以将其保存在数据库中.
我有两个模型,homemodel处理我的数据库,image_upload_model处理图像上传.
一切正常,但我不知道如何将图像文件名发布到数据库

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

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

任何帮助表示赞赏,谢谢!

你可以得到这样的文件名
$upload_data = $this->upload->data(); 
  $file_name =   $upload_data['file_name'];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读