Reputation: 703
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
Upvotes: 1
Views: 4868
Reputation: 690
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.
Upvotes: 1
Reputation: 38
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);
});
Upvotes: 0