ThunderBirdsX3
ThunderBirdsX3

Reputation: 588

Get file value before upload

How can i get file value before upload? This is example code.

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

This is controller.

           ...

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

          ...
       }

I try to show "userfile" value, But it alway show null. How can i show this value.

Upvotes: 0

Views: 8213

Answers (1)

G. Vega
G. Vega

Reputation: 78

If you need get file name...

  1. Before click button onSubmit... you have work in javascript

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

<script>
function changeEventHandler(event){
    alert(event.target.value);
}
</script>
  1. After click button onSubmit... you have work in server... Controller 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'];

      ...
   }

... or use PHP

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

Upvotes: 2

Related Questions