上传前获取文件值

Get file value before upload

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

<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>

这是控制器。

           ...

       public function do_upload()
       {
          ///////////////////////////////////////////////
          // Hear i want to show filename before upload.
          ///////////////////////////////////////////////
          echo var_dump( $this->input->post('userfile') );
          ///////////////////////////////////////////////

          ...
       }

我尝试显示 "userfile" 值,但它总是显示空值。我如何显示这个值。

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

  1. 在单击提交按钮之前...您在 javascript
  2. 中有工作

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

<script>
function changeEventHandler(event){
    alert(event.target.value);
}
</script>
  1. 单击提交按钮后...您在服务器中工作...控制器 CodeIgniter...

...

   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'];