繁体   English   中英

Codeigniter上传文件名

[英]Codeigniter upload file name

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

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

在 data() 中使用 file_name ...最终代码将是

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM