通常我们可以通过使用$this->input->get('field_name')
或$this->input->post('field_name')
来获取Codeigniter
中的表单数据,这很好。
在原始的PHP
中,我们使用$_FILES["fileToUpload"]["name"]
来获取用户试图上传的文件名。
我的问题是:是否有Codeigniter
方法可以获取需要上传的文件的名称?
我想说的是,我需要在尝试使用Codeigniter
库(而不是使用原始PHP
全局$_FILES
变量)将用户试图上传的文件保存到我的服务器之前,获得该文件的名称。
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
// get the user submitted file name here
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
2条答案
按热度按时间uujelgoq1#
这是2和3的2个版本文档
如果要在后端获取文件名:
$this->upload->file_name
它将基于system/library/upload.php
这个函数工作。如果需要获取文件名...
在保存到服务器之前...您必须在javascript中进行工作
<?php echo "<input type='file' name='userfile' size='20' onchange='changeEventHandler(event);' />"; ?>
javascript中的onchange事件:
zfciruhq2#