I have some problem in validating limitation of file size on uploading files in codeigniter.
Here's my code
public function upload_docu_by_type() {
ini_set('max_execution_time', 300);
$this->load->helper(array('form', 'url'));
$F = array();
$prop_id = sanitize($this->input->post('prop_id'));
$file = sanitize($this->input->post('file'));
$config = array();
$config['upload_path'] = './/assets//img//documents//';
$config['allowed_types'] = 'jpeg|jpg|png|pdf';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$this->load->library('upload');
$files = $_FILES;
for($i=0; $i< count($files['images']['name']); $i++) {
$config['file_name'] = $prop_id."_".$file."_".$i;
// $_FILES['images']['name']= time()."_".$files['images']['name'][$i];
$_FILES['images']['name']= time()."_".preg_replace('/[()]/','',$files['images']['name'][$i]);
// $_FILES['images']['name']= $prop_id."_".$file;
$_FILES['images']['type']= $files['images']['type'][$i];
$_FILES['images']['tmp_name']= $files['images']['tmp_name'][$i];
$_FILES['images']['error']= $files['images']['error'][$i];
$_FILES['images']['size']= $files['images']['size'][$i];
$this->upload->initialize($config);
if (! $this->upload->do_upload('images')){ //if not
$errors = array('error' => $this->upload->display_errors());
$data = array('success' => 0, 'errors' => $errors);
}
else if($_FILES['images']['size'] >= 5000){
$data = array("success" => "1", 'message' => "You've reached the limit");
}else{
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$file_type = $upload_data['file_type']; //this will be the name of your file
$img = array('uploader_file' => $file_name);
$this->Applicant_model_w->upload_docu_by_type($prop_id,$file_name,$file_type,$file);
$data = array("success" => "1", 'message' => "Successfully uploaded file/s.");
}
}
generate_json($data);
}
I tried
else if($_FILES['images']['size'] >= 5000){
$data = array("success" => "1", 'message' => "You've reached the limit");
}
but it doesn't work. I'm just new in Codeigniter.
What would be the proper way to validate file size limitation? I'm using Code Igniter
You can add limit to size in image uploading for height and width like below
$image_info = getimagesize($_FILES["images"]["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1];
if ($image_width >= 1900 && $image_height >= 600)
{
$this->upload->initialize($config);
if (! $this->upload->do_upload('images')){ //if not
$errors = array('error' => $this->upload->display_errors());
$data = array('success' => 0, 'errors' => $errors);
}
}
And if you want to just limit by size
if ($image_info >= 5000)
{
$this->upload->initialize($config);
if (! $this->upload->do_upload('images')){ //if not
$errors = array('error' => $this->upload->display_errors());
$data = array('success' => 0, 'errors' => $errors);
}
}
Here is how exactly CodeIgniter code works:
<?php
$config['upload_path'] = $this->path . "images/";
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = 0; //3 * 1024 * 1024; //3Mb; 0=unlimited
$config['max_width'] = 0;//"3850"; //'1024';
$config['max_height'] = 0;//"2180"; //'768';
$config['max_filename'] = 30; //0=no limit in length or size
$config['encrypt_name'] = TRUE; //TRUE=means file name will be converted to a random encrypted string
$config['remove_spaces'] = TRUE; //TRUE=means spaces in the file name will be converted to underscores
$this->load->library('upload', $config);
if(!$this->upload->do_upload("img")){
$error = array('error' => $this->upload->display_errors());
echo json_encode(array('status'=>'error', 'message'=>$error));
}else{
$data = array('upload_data' => $this->upload->data());
echo json_encode(array(
"status" => "success",
"url" => $this->path . "images/" . $data["upload_data"]["file_name"],
"width" => $data["upload_data"]["image_width"],
"height" => $data["upload_data"]["image_height"],
"data" => $data["upload_data"],
"message" => 'You have successfully uploaded the image.'
));
}
?>