【问题标题】:codeigniter file upload check file size fails with an error numbercodeigniter 文件上传检查文件大小失败并出现错误号
【发布时间】:2019-04-23 22:37:19
【问题描述】:

我正在尝试上传最大 2MB 大小的图片。我正在尝试使用 6.44MB 的图像来检查测试用例。如果图像大小超过 2MB,上传者应该会收到相关消息。 我的表格是:

<?php echo form_open_multipart('Addthepic');?>
<table>
  <tr>
     <td><input type="file" name="image">(Dimension should be 370*234)</td>
     <td><input type="text" name="alt_text" placeholder="Alternate Text"></td>
     <td><input type="text" name="title" placeholder="Title"></td>
     <td><input type="text" name="caption" placeholder="Caption"></td>
     <td><input type="submit" name="submit" class="btn btn-success" value="Add Now"></td>
  </tr>
</table>
<?php echo form_close();?>

我的模型中的代码是:

if(!empty($_FILES['image']['name']) && $_FILES['image']['size']>2097152)
{
    return "<div class='alert alert-danger'>Max 2MB file is allowed for image.</div>";
}
else
{
    var_dump($_FILES['image']);
    $msg.="<div class='alert alert-success'>".$_FILES['image']['error']."</div>";
    $config1=array(
        'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/assets/uploads/eimg/",
        'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
        'overwrite'  => TRUE,
        'file_name' =>$filename
    );
    $this->load->library('upload',$config1);
    $this->upload->overwrite = true;
    if($this->upload->do_upload('image'))
    {
        $image_data =   $this->upload->data();
        $configer1 =  array(
          'image_library'   => 'gd2',
          'source_image'    =>  $image_data['full_path'],
          'maintain_ratio'  =>  FALSE,
          'width'           =>  370,
          'height'          =>  234,
          'overwrite'       =>  TRUE,
          'file_name'       =>  $filename
        );
        $this->image_lib->clear();
        $this->image_lib->initialize($configer1);
        $this->image_lib->resize();
        $this->db->where('sno',$sno);
        $this->db->update('events',array('image'=>$filename));
        if($this->db->affected_rows()>0)
            $msg.= "<div class='alert alert-success'>Image has been uploaded successfully</div>";
    }
}

eimg 目录在服务器上的权限是 0777

var_dump 给出以下输出:

array(5) { ["name"]=> string(12) "Imgname.JPG" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(1) ["size"]=> int(0) }

$_FILES['image']['error'] 给出

1

$_FILES['image']['size'] 给出

0

$_FILES['image']['name'] 正确显示文件名

【问题讨论】:

  • 这不是 CodeIgniter 的问题
  • 这个问题是上传的文件超出了php.ini中的upload_max_filesize指令。阅读here 以找到解决方案。
  • @SherifSalah 不正确。 1的错误码表示upload_max_filesize的问题。阅读here了解更多信息。
  • 哦,抱歉,我刚刚阅读了权限消息,并认为错误,甚至没有注意到他说权限是 0777 .. 我的错。
  • @marvinlsSacul 我也采用相同的方式,但是当名称被 $_FILES 识别时,它也应该显示大小。到目前为止,我们还没有尝试上传文件。稍后会触发上传。如果不是这样,那么通知用户无效文件大小的方法是什么?

标签: php codeigniter file-upload


【解决方案1】:

根据 php 文档,错误代码 1 表示文件超出了设置的服务器上传限制。由于您只是想通知用户这个文件大小限制超出的问题,您可以使用 1 来检查您的 php 代码,如下所示:

if(!empty($_FILES['image']['name']) && ($_FILES['image']['error']==1 || $_FILES['image']['size']>2097152))
{
    return "<div class='alert alert-danger'>Max 2MB file is allowed for image.</div>";
}

其中,2 MB 是您在 php.ini 中或通过您的 php 代码中的 ini 设置的限制。

【讨论】:

    【解决方案2】:

    你的代码没有做错,错误值为 1 是指this

    是由于 php 的限制和您可以上传的图片最大尺寸的条件。

    为了向想要上传比您在模型中定义的尺寸更大的图像的用户显示警告,您可以使用 jquery 来完成。像这样的。

    HTML

    <input type="file" id="myImage" name="image" />
    

    jQuery

    $('#myImage').bind('change', function() {
    
      //this.files[0].size
      alert(this.files[0].size);
    
    });
    

    【讨论】:

      猜你喜欢
      • 2022-08-24
      • 2016-08-02
      • 1970-01-01
      • 2019-07-03
      • 2020-06-12
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      相关资源
      最近更新 更多