上传前获取文件值

时间:2016-04-15 13:01:47

标签: codeigniter file-upload codeigniter-3

如何在上传之前获取文件值?这是示例代码。


SpringExtensionImpl springExtensionImpl;
actorSystem.actorOf(springExtensionImpl.create(Test.class, account, order));

这是控制器。

<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?> <!-- Error Message will show up here -->
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>

我尝试显示“userfile”值,但它总是显示null。我怎样才能显示这个价值。

1 个答案:

答案 0 :(得分:1)

如果您需要获取文件名......

  1. 点击按钮onSubmit ...你有javascript工作
  2. <?php echo "<input type='file' name='userfile' size='20' onchange='changeEventHandler(event);' />"; ?>

    <script>
    function changeEventHandler(event){
        alert(event.target.value);
    }
    </script>
    
    1. 单击按钮onSubmit ...您已在服务器中工作... Controller CodeIgniter ...
    2. ...

         public function do_upload()
         {
            $config['upload_path']   = './uploads/'; 
            $config['allowed_types'] = 'gif|jpg|png'; //if your file is image
            $config['max_size']      = 100; 
            $config['max_width']     = 1024; 
            $config['max_height']    = 768;  
            $this->load->library('upload', $config);
            $data = $this->upload->data()
            echo $data['file_name'];
      
            ...
         }
      

      ...或使用PHP

      $name_file = $_FILES['userfile']['name'];