douchun6108 2017-08-14 05:55
浏览 58

Codeigniter上传文件名

Usually we can get the form data in Codeigniter by using $this->input->get('field_name') or $this->input->post('field_name') and that's fine.

In raw PHP we use $_FILES["fileToUpload"]["name"] to get the file name that the user trying to upload.

My question is: Is there any Codeigniter way to get the name of the file that needs to be uploaded?

I am trying to say that i need to get the file name that the user is trying to upload before trying to save it in my server using Codeigniter library instead of using raw PHP global $_FILES variable.

<?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);
                }
        }
}
?>
  • 写回答

1条回答 默认 最新

  • doucong8553 2017-08-14 05:58
    关注
    $upload_data = $this->upload->data(); 
    $file_name =   $upload_data['file_name'];
    

    Here is the 2 version doc is for 2 and for 3

    if you want to get the file name in backend:

    $this->upload->file_name It will work based on system/library/upload.php this function.

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

    If you need to get file name...

    Before saving to server... you have work in javascript

    <?php echo "<input type='file' name='userfile' size='20' onchange='changeEventHandler(event);' />"; ?>

    onchange event in javascript:

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

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流