How to validate upload file only pdf - validation

I would like to validate file upload, the condition is file input can upload file if file input is empty, but if file input does not match the type of file setting then the error showing. How to make these error. if I using this script : !$this->upload->do_upload('file') and i let file input empty, certainly the error are showing.
this is my Config upload :
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}

You could add a condition, so it will only validate if there are uploaded file :
<?php
if ( $_FILES && $_FILES['file']['name'] )
{
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
}

This solution will work perfectly for you
$config['upload_path'] = './uploads/;
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = round(100,99); //make your file name random
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
print $this->upload->data();
}else{
print $this->upload->display_errors();
}

Related

Multiple image uploading error in Codeigniter

I am trying to upload the multiple images through my form. I have taken care of many expect like form_open_multipart, calling a name as Array in upload input.
I have no issue with the images upload. I can upload multiple images successfully, but the problem comes when I select any text or non-image file with the images.
I am checking scenarios that if user mixes it up (accidently select any other file with images) images with text file then some files are getting uploaded and then I get the error of the wrong type of file.
In Ideal case, it may not allow uploading the file if any one file does not have the correct file type.
so can any one help me over that how can I check before file upload that all files are allowed type or not?
I tried many examples but get the same problem In all.
There is no problem if i upload the multiple images. It's work fine.
<input type="file" name="pic_url[]" multiple accept="image/*" />
And this below is my controller
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
}
First problem is primarily down to how you handle your errors.
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
The above code will simply halt the rest of the for loop and redirect on the first error.
A simple solution would be to change that to log failures and continue processing:
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
$upload_error[$i] = array('error' => $this->upload->display_errors());
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Alternatively, you would need to look at manually validating files based on their file extension.
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
foreach ($_FILES['pic_url'] as $key => $file) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
if (strpos($config['allowed_types'], $ext) === false) {
$upload_error[$key] => array('error' => sprintf('<p>Invalid file extension (%s)</p>', $ext));
continue;
}
$_FILES['userfile'] = $file;
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$upload_error[$key] = array('error' => $this->upload->display_errors());
} else {
// do something with $this->upload->data();
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Hope that helps

While Uploading Image don't want to save actual image name with space

In codeigniter while I uploading the image and store image name in database If actual image name is like "abc data.jpg" and I want to store image name like abc_data.jpg and also with this name image should move in uploads folder.
This is my image Upload Code:-
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('cover_image'))
{
$data =$this->upload->data();
$data_ary = array(
'project_id' => (int)$iProjectId,
'image_url' => $data['file_name'],
'is_covred_photo' => 'YES'
);
$this->db_project->insert( $this->sTable6 , $data_ary);
$aResp = array( 'project_images_id' => $this->db->insert_id());
}
Add $config['file_name']
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$config['file_name'] = 'My_new_file_name'; # Add here
$this->load->library('upload', $config);
remove this line as well
$this->upload->initialize($config);
Try this:
$file_name = $_FILES['file_view_name']['name'];
$file_name_pieces = split('_', $file_name);
$new_file_name = '';
$count = 1;
foreach($file_name_pieces as $piece)
{
if ($count !== 1)
{
$piece = ucfirst($piece);
}
$new_file_name .= $piece;
$count++;
}
$config['file_name'] = $new_file_name;
Refer this

My file type not saving on my database

I have completed file upload but if the file name is "213134.jpg" but my code store database only name "213134" without file type. Can you please help me. I have used CI for this task
function postMessage(){
$config['upload_path'] = './uploads/inbox/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random;
$config['file_name'] =$pic;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$blog_msg ='Sorry, Picture uploaded.';
}
$u_id=$this->session->userdata('id');
$u_name=$this->session->userdata('u_fname');
$to=$this->uri->segment(3);
$date= $date=date('d-M-Y');
$message=$_POST["message"];
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$pic
);
}
$config['allowed_types'] = 'jpg|jpeg';//Change this
Method 01
$data=array(
'm_from'=>$u_id,
'from_name'=>$u_name,
'm_date'=>$date,
'm_body'=>$message,
'm_to'=>$to,
'm_attach'=>$this->input->post('pic'), //change this(in your HTML attribute name='' )
);
Method 02
or else you can try
$ext = pathinfo($pic, PATHINFO_EXTENSION); //Get the Extension
$random=rand(00000, 99999);
$id=$this->session->userdata('id');
$pic=$id*$random.$ext;//change this
$config['file_name'] =$pic;

CodeIgniter upload multiple files to multiple directories

$configUpload = array();
$configUpload['upload_path'] = './directory_1/';
$configUpload['max_size'] = '6000';
$configUpload['max_width'] = '9500';
$configUpload['max_height'] = '9500';
$configUpload['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$this->load->library('upload',$configUpload);
$upload_1 = $this->upload->do_upload('product_image');
if($upload_1 === FALSE)
$product_data = $this->upload->data();
continue;
$config = array();
$config['upload_path'] = './directory_2/';
$config['max_size'] = '5000';
$config['max_width'] = '10000';
$config['max_height'] = '10000';
$config['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$this->load->library('upload', $config);
$upload = $this->upload->do_upload('product_image_2');
When I submit to upload, image files are in the same directory (directory_1).
Need To initialize your class
$configUpload = array();
$configUpload['upload_path'] = 'uploads/canvas_uploads/upload_save/';
$configUpload['allowed_types'] = 'png|jpg|jpeg|gif|bmp';
$configUpload['file_name'] = date("Ymd") . time();
$configUpload['overwrite'] = true;
$this->load->library('upload', $configUpload);
$this->upload->initialize($configUpload);
$upload_1 = $this->upload->do_upload('file_input');
$config['upload_path'] = 'uploads/canvas_uploads';
$config['allowed_types'] = 'jpg|png|jpeg|gif|bmp|tif';
$config['file_name'] = date("Ymd") . time();
$config['overwrite'] = true;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload("file_input");

how to define 2 config upload file in codeigniter

I want to upload 2 file with 2 config section but first file don't work and second file work good for me. how can i find problem in this code.
Please help me.
This is my code :
$this->load->library('upload');
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '200';
$config['max_height'] = '200';
$config['file_name'] = time();
$this->upload->initialize($config); // Important
if ($this->upload->do_upload('logo')) {
$logo = $this->upload->data();
} elseif ($this->input->post('logo')) {
$logo['file_name'] = $this->input->post('logo');
} else {
$logo['file_name'] = '';
}
//===================================================
$config['upload_path'] = './uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '950';
$config['max_height'] = '250';
$config['file_name'] = time();
$this->upload->initialize($config); // Important
if ($this->upload->do_upload('header')) {
$header = $this->upload->data();
} elseif($this->input->post('header')) {
$header['file_name'] = $this->input->post('header');
} else {
$header['file_name'] = '';
}

Resources