我试图在用户上传时限制文件大小。我有以下功能:
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数组,它会显示文件太大的错误,这就是我想要的。但问题是如果我不打印它,即使图片太大,整个过程也会完成,并且数据库中的数据已经完成。我该如何解决这个问题?
答案 0 :(得分:0)
你的回归不在其他地方。你想要:
return $fullImagePath;
位于
之下 $fullImagePath = '/uploads/' . $upload_data['file_name'];
否则它将返回,无论它是否有错误,我猜测它会被发送到您的数据库..?