You did not select a file to upload CodeIgniter

2019-08-06 05:36发布

问题:

$this->upload->data() result is

Array
(
    [file_name] => 72f59510f9bbf05933c89e4951acc29d
    [file_type] => 
    [file_path] => ./inst/public/uploads/
    [full_path] => ./inst/public/uploads/72f59510f9bbf05933c89e4951acc29d
    [raw_name] => 72f59510f9bbf05933c89e4951acc29d
    [orig_name] => 
    [client_name] => 
    [file_ext] => 
    [file_size] => 
    [is_image] => 
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
)

error:

Array
(
    [error] => You did not select a file to upload.
)

upload function

    function upload(){

     if(isset($_POST['userfile']) AND !empty($_POST['userfile']))

     {

         $Info = $this->login();
         if(@$Info)

         {

             $config['upload_path']    = './inst/public/uploads/';
             $config['allowed_types']  = 'gif|jpg|png';
             $config['max_size']       = '1000';
             $config['max_width']      = '230';
             $config['max_height']     = '280';
             $config['min_width']      = '220';
             $config['min_height']     = '270';
             $config['remove_spaces']  = TRUE;
             $config['overwrite'] = TRUE;
             $config['file_name']     = md5(uniqid("100_ID", true));


             $this->load->library('upload', $config);

                   $Setting = $this->Setting;
                   $this->load->view('header',$Setting);


             if ( ! $this->upload->do_upload("userfile"))
                 {
                      $response['error'] = array('error' => $this->upload->display_errors());
                      echo '<pre>';
                      print_r( $this->upload->data());
                      $this->load->view('upload_done', $response);
                 }

             else
                 {
                      $response['success'] = array('upload_data' => $this->upload->data());
                      $this->load->view('upload_done', $response);
                 }

         }

     }

}

form code

<?php 
    echo form_open('/Home/upload');
?>
                    <br><div class="form-group"><input class ='form-control' placeholder="<?php echo lang('fileu'); ?>" type="file" name="userfile" size="20" /></div>

    <div class="alert alert-info"><?php echo lang('filetext'); ?></div>


  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo lang('Close'); ?></button>
    <button type="submit" class="btn btn-primary"><?php echo lang('uploadsub'); ?></button>
    <?php echo form_close(); ?>

回答1:

you have to use below code to upload files. You are missing multipart attribute in your form.

echo form_open_multipart('/Home/upload');


回答2:

First change your helper function to form_open_multipart(). If you still get the error after changing to the correct function, it could be your maxsize property as well .If an uploaded file is larger than the allowable size, the FILES variable will be empty.

Try changing...

$config['max_size']       = '1000';

...to something like...

 $config['max_size']       = '30000';

php.net File Upload