1

I am trying to upload image with a check of max 2MB size. I am trying an image of 6.44MB to check the test case. If image size if more than 2MB, the uploader should get relevant message. My Form is:

<?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();?>

The code in my Model is:

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>";
    }
}

Permissions of eimg directory is 0777 on server

var_dump gives the following output:

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

$_FILES['image']['error'] gives

1

$_FILES['image']['size'] gives

0

$_FILES['image']['name'] shows the file name correctly

ITSagar
  • 673
  • 2
  • 10
  • 29
  • this is not a CodeIgniter problem – Kagiso Marvin Molekwa Nov 21 '18 at 11:24
  • 1
    this problem is that the uploaded file exceeds the `upload_max_filesize` directive in php.ini. read [here](https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size) to find a solution. – Kagiso Marvin Molekwa Nov 21 '18 at 11:29
  • 1
    @SherifSalah that is incorrect. the error code of `1` indicates the `upload_max_filesize` problem. read [here](https://secure.php.net/manual/en/features.file-upload.errors.php) for more info. – Kagiso Marvin Molekwa Nov 21 '18 at 11:30
  • Ohhh sorry i just read the permission message and thought that the error and didn't even notice he was saying the permission is 0777 .. my bad. – Sherif Salah Nov 21 '18 at 11:35
  • @marvinlsSacul I also though the same way but when the name is recognized by $_FILES, it should also show the size. Till this point, we are not trying to upload the file. Upload is triggered later on. If thats not the case, then what is the way to inform user about invalid file size? – ITSagar Nov 21 '18 at 11:39

2 Answers2

1

as per php docs, error code 1 means file is exceeding the set server upload limits. Since you just want to inform the user about this problem of file size limit exceeding, you can just use 1 for checking in your php code as below:

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>";
}

Where, 2 MB is the limit that your had set in php.ini or through ini set inside your php code.

Shobhit Gupta
  • 690
  • 4
  • 13
0

you do nothing wrong in your code, error has value 1 is refer to this

is from php limitation and your condition of max size of the image that can be uploaded.

For showing the warning to the user that want upload an image that have bigger size than you define in your model, you can do it with jquery. something like this.

HTML

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

jQuery

$('#myImage').bind('change', function() {

  //this.files[0].size
  alert(this.files[0].size);

});