0

我试图在用户上传时限制文件大小。我有以下功能:

function do_upload()
    {
        $config['upload_path']   = './uploads/';        
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '50';
        //$config['max_width'] = '1024';
        //$config['max_height'] = '768';

        $this->load->library('upload', $config);
        $fullImagePath;
        if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name']))
        {
              if (! $this->upload->do_upload('userfile'))
              {
                   $error = array('file_error' => $this->upload->display_errors());
                   //print_r($error);

                   $this->load->view('layout/header');
                   $this->load->view('register', $error);
                   $this->load->view('layout/footer');

              }else{
                  // set a $_POST value for 'image' that we can use later
                  $upload_data    = $this->upload->data();
                  $fullImagePath = '/uploads/' . $upload_data['file_name']; 
              }
        }       
        //path of image on the server
        return $fullImagePath;
    }

如果我打印上面的 $error 数组,它会显示文件太大的错误,这正是我想要的。但问题是,如果我不打印,即使图片太大,整个过程也完成了,并且数据库中的数据是完整的。我怎样才能解决这个问题?

4

1 回答 1

0

你的回报是在 else 之外。你要:

return $fullImagePath;

在下面

$fullImagePath = '/uploads/' . $upload_data['file_name'];

否则无论是否出错它都会返回,我猜它会被发送到您的数据库..?

于 2013-04-26T10:37:46.523 回答