【问题标题】:codeigniter alert if image size exceeds如果图像大小超过,codeigniter 会发出警报
【发布时间】:2016-06-11 09:02:49
【问题描述】:

您好,我有上传图片的代码。我可以完美地添加图像,但我想添加限制,如果图像大小超过,则会弹出一个警报,但我不知道如何添加我在图像大小超过时发出警报。请帮帮我

这是我的代码:

控制器:

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


}

【问题讨论】:

    标签: php image codeigniter


    【解决方案1】:

    要上传文件,您必须使用 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="多部分/表单数据"。它指定要使用的内容类型 提交表单时

    没有上述要求,文件上传将无法进行。

    【讨论】:

    • 我没有使用帖子。
    • 那你用的是什么?
    猜你喜欢
    • 2020-10-16
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2022-01-17
    • 2016-11-20
    • 1970-01-01
    相关资源
    最近更新 更多