php Codeigniter:如何获取文件的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15684885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter: how to get a filename of a file
提问by ignatius nash
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
我是 Codeigniter 新手,我正在尝试获取上传图像的文件名,以便将其保存在数据库中。我有两个模型,homemodel 处理我的数据库,而 image_upload_model 处理图像上传。一切正常,除了我不知道如何将图像文件名发布到数据库
image_upload_model.php
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
<?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);
}
}
?>
The controller
homecontroller.php
控制器
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');
}
}
?>
any help is appreciated, thank you!
任何帮助表示赞赏,谢谢!
回答by It worked yesterday.
You can get file name like this
你可以得到这样的文件名
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
回答by Marc Audet
On a very high level, you need to restructure your code as follows:
在非常高的层次上,您需要按如下方式重构代码:
(1) In your HomeController
, upload the image first ($this->Image_upload_model->do_upload()
and then update your database ($this->homemodel->addTenants()
(1) 在你的 中HomeController
,先上传图片($this->Image_upload_model->do_upload()
然后更新你的数据库($this->homemodel->addTenants()
(2) In your upload model, you need to invoke $this->upload->data()
to get the information array that contains your file name (see CodeIgniter documentation). You then have to get that file name and make it available to the HomeController and pass it to the addTenants
function.
(2) 在您的上传模型中,您需要调用$this->upload->data()
以获取包含您的文件名的信息数组(参见 CodeIgniter 文档)。然后,您必须获取该文件名并将其提供给 HomeController 并将其传递给addTenants
函数。
With this guidance, you should be able to modify your code accordingly.
有了这个指导,您应该能够相应地修改您的代码。
回答by dungthantai
Easy to get file name with $this->upload->file_name
轻松获取文件名 $this->upload->file_name
based on function upload in system/library/upload.php
基于函数上传 system/library/upload.php
public $file_name = "";
public function data()
{
return array (
'file_name' => $this->file_name,
'file_type' => $this->file_type,
...
);
}