如果图像大小超过,则发出代码点火器警报


codeigniter alert if image size exceeds

嗨,我有这个代码来上传图像。我可以完美地添加图像,但我想添加限制,如果图像大小超过,会弹出一个警报,但我不知道如何添加如果图像大小超过我警报。请帮助我

这是我的代码:

控制器:

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() */

}

要上传文件,您必须使用 POST 方法。 阅读此 W3Schools 文件上传。

 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="multipart/form-data"。它指定要使用的内容类型 提交表格时

如果没有上述要求,文件上传将不起作用。