Codeigniter上载文件名

vybvopom  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(161)

通常我们可以通过使用$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);
                }
        }
}
?>
uujelgoq

uujelgoq1#

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

这是23的2个版本文档
如果要在后端获取文件名:
$this->upload->file_name它将基于system/library/upload.php这个函数工作。

public function data()
{
    return array (
                    'file_name'         => $this->file_name,
                    'file_type'         => $this->file_type,
                    ...
                );
}

如果需要获取文件名...
在保存到服务器之前...您必须在javascript中进行工作
<?php echo "<input type='file' name='userfile' size='20' onchange='changeEventHandler(event);' />"; ?>
javascript中的onchange事件:

<script>
function changeEventHandler(event){
    alert(event.target.value);
}
</script>
zfciruhq

zfciruhq2#

$data = array('upload_data' => $this->upload->data());

// use file_name within the data() the final code will be 

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

相关问题