您没有选择要上传CodeIgniter的文件

时间:2014-02-07 16:23:41

标签: php codeigniter upload

$this->upload->data()结果是

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] => 
)

错误:

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

上传功能

    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);
                 }

         }

     }

}

表单代码

<?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(); ?>

2 个答案:

答案 0 :(得分:4)

您必须使用以下代码上传文件。您在表单中缺少multipart属性。

echo form_open_multipart('/Home/upload');

答案 1 :(得分:0)

首先将助手功能更改为form_open_multipart()。如果在更改为正确的函数后仍然出现错误,则它也可能是您的maxsize属性。如果上载的文件大于允许的大小,则FILES变量将为空。

尝试更改......

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

......类似......

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

php.net File Upload