您好我有上传图片的代码。我可以完美地添加图像,但我想添加限制,如果图像大小超过,将会弹出警报,但我不知道如果图像大小超过,我可以添加警报。请帮帮我
这是我的代码:
控制器:
public function upload_file()
{
$filename = 'r_image';
$status = "";
$msg = "";
$config['upload_path'] = 'img/recipes/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024 * 8 ;
$config['encrypt_name'] = true;
$this->load->library('upload',$config);
if (!$this->upload->do_upload($filename))
{
$stats = 'error';
$msg = $this->upload->display_errors('', '');
}else{
$uploadData = $this->upload->data('file_name');
if($uploadData['file_name'])
{
$thumnail = 'img/recipes/'.$uploadData['file_name'];
}else{
$thumnail = 'No thumnbail uploaded!';
}
$updateThumbData = array('recipe_id' => $recipe_id,
'r_image' => $thumnail
);
$this->products_model->updataRecipeThumnail($updateThumbData);
redirect('dashboard/view_product');
} /* upload_file() */
}
答案 0 :(得分:0)
要上传文件,您必须使用 POST 方法。阅读此w3schools file upload。
if($_FILES['Your_POST_name']['size']>1024 * 8 )// to check what is your post name print_r($_FILES);
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger alert-dismissible fade in" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Image is too big!</div>');
redirect('dashboard/view_product');
}
并在view_product的视图文件中,添加以下代码以显示错误
<?php echo $this->session->flashdata('msg'); ?>
在php中处理文件上传时要记住的重要事项。
确保表单使用method =“post”
表单还需要以下属性: ENCTYPE =“多部分/格式数据”。它指定要使用的内容类型 提交表格时
如果没有上述要求,文件上传将无效。