codeigniter file upload check file size fails with an error number
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
php codeigniter file-upload
add a comment |
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
php codeigniter file-upload
this is not a CodeIgniter problem
– marvinIsSacul
Nov 21 '18 at 11:24
1
this problem is that the uploaded file exceeds theupload_max_filesize
directive in php.ini. read here to find a solution.
– marvinIsSacul
Nov 21 '18 at 11:29
1
@SherifSalah that is incorrect. the error code of1
indicates theupload_max_filesize
problem. read here for more info.
– marvinIsSacul
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
add a comment |
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
php codeigniter file-upload
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
php codeigniter file-upload
php codeigniter file-upload
asked Nov 21 '18 at 11:02
ITSagarITSagar
271113
271113
this is not a CodeIgniter problem
– marvinIsSacul
Nov 21 '18 at 11:24
1
this problem is that the uploaded file exceeds theupload_max_filesize
directive in php.ini. read here to find a solution.
– marvinIsSacul
Nov 21 '18 at 11:29
1
@SherifSalah that is incorrect. the error code of1
indicates theupload_max_filesize
problem. read here for more info.
– marvinIsSacul
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
add a comment |
this is not a CodeIgniter problem
– marvinIsSacul
Nov 21 '18 at 11:24
1
this problem is that the uploaded file exceeds theupload_max_filesize
directive in php.ini. read here to find a solution.
– marvinIsSacul
Nov 21 '18 at 11:29
1
@SherifSalah that is incorrect. the error code of1
indicates theupload_max_filesize
problem. read here for more info.
– marvinIsSacul
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
this is not a CodeIgniter problem
– marvinIsSacul
Nov 21 '18 at 11:24
this is not a CodeIgniter problem
– marvinIsSacul
Nov 21 '18 at 11:24
1
1
this problem is that the uploaded file exceeds the
upload_max_filesize
directive in php.ini. read here to find a solution.– marvinIsSacul
Nov 21 '18 at 11:29
this problem is that the uploaded file exceeds the
upload_max_filesize
directive in php.ini. read here to find a solution.– marvinIsSacul
Nov 21 '18 at 11:29
1
1
@SherifSalah that is incorrect. the error code of
1
indicates the upload_max_filesize
problem. read here for more info.– marvinIsSacul
Nov 21 '18 at 11:30
@SherifSalah that is incorrect. the error code of
1
indicates the upload_max_filesize
problem. read here for more info.– marvinIsSacul
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
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
@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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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);
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410712%2fcodeigniter-file-upload-check-file-size-fails-with-an-error-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 11:36
Shobhit GuptaShobhit Gupta
535211
535211
add a comment |
add a comment |
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);
});
add a comment |
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);
});
add a comment |
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);
});
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);
});
answered Nov 22 '18 at 9:22


Rizki MasjahriRizki Masjahri
214
214
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410712%2fcodeigniter-file-upload-check-file-size-fails-with-an-error-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
this is not a CodeIgniter problem
– marvinIsSacul
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 to find a solution.– marvinIsSacul
Nov 21 '18 at 11:29
1
@SherifSalah that is incorrect. the error code of
1
indicates theupload_max_filesize
problem. read here for more info.– marvinIsSacul
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