How to update pdf file and image file in codeigniter - php

I want to upload PDF file as well as image file with on one do_upload
I want to upload two different files in two different directory by using codeigniter. I wrote the following code in my model. but it will upload only the first image.
if($_POST){
if($_FILES['productimage']['name']){
$img = $_FILES['productimage']['name'];
$config['upload_path'] = './uploads/products/';
$config['allowed_types'] = 'png|jpg|gif|bmp';
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
if(!$this->upload->do_upload('productimage'))
{
$errors = array('error' => $this->upload->display_errors());
$img="";
}
else
{
$data =$this->upload->data();
$img=$data['file_name'];
//print_r($img);die;
}
}else{
$img=$this->input->post('image_old');
}
if($_FILES['productpdf']['name']){
$img = $_FILES['productpdf']['name'];
$config['upload_path'] = './uploads/products/';
$config['allowed_types'] = 'png|jpg|gif|bmp|pdf';
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
if(!$this->upload->do_upload('productpdf'))
{
$errors = array('error' => $this->upload->display_errors());
$pdf="";
}
else
{
$data =$this->upload->data();
$pdf=$data['file_name'];
}
}else{
$pdf=$this->input->post('pdf_old');
}
// print_r($img);print_r($pdf);die;
$title = $this->input->post('productname');
$content = $this->input->post('description');
$status = $this->input->post('status');
$this->db->where('product_id', $id);
$this->db->update('products',array('product_name'=>$title,'product_content'=>$content,'product_image'=>$img,'product_file'=>$pdf,'product_status'=>$status));
$this->db->where('product_id',$id);
$this->db->delete('products_filter');
$filters= $_POST['filter'];
foreach ($filters as $value)
{
$this->db->insert('products_filter',array('product_id' => $id,'products_search_id'=>$value));
}
return ($this->db->affected_rows() != 1)? false:true;
}else{
redirect(base_url('admin/products/product-list/'.$redirectid));
}
}

Please check my code
this code to handle pdf or images file upload in different directory
<?php
if($_POST){
if($_FILES['productimage']['name'])
{
$custom_file_name = $_FILES['productimage']['name'];
$file_ext = end(explode('.', $custom_file_name));
if($file_ext=='pdf')
{
$upload_path_directory='./uploads/products/pdf/';
$file_field_name="productimage";
}
else{
$upload_path_directory='./uploads/products/images/';
$file_field_name="productpdf";
}
$config['upload_path'] = $upload_path_directory;
$config['allowed_types'] = 'png|jpg|gif|bmp|pdf';
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
if(!$this->upload->do_upload("$file_field_name"))
{
$errors = array('error' => $this->upload->display_errors());
$custom_file_name="";
}
else
{
$data =$this->upload->data();
$custom_file_name=$data['file_name'];
}
?>
First to make sure to upload library libraries and form validation if don't know where to set libaray will mention below*
$this->load->libaray("upload");
$this->load->libaray("form_validation");
to add this code in if condition

Related

Issue with saving contents of the uploaded text file in the database field

//my model test.php is as follows
public function saveallpars($id)
{
$config['upload_path'] = sys_get_temp_dir();
$config['allowed_types'] = 'txt';
$config['xss_clean'] = false;
$config['overwrite'] = true;
$config['return_temp_file'] = true;
$this->load->library('upload', $config);
if (!$file = $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
dd($error);
}
else
{
$contents = explode("\n", file_get_contents($file));
$this->data= serialize($contents);
$this->db->where(array('id' => $id))->set(array('data' =>$this->data))->update($this->_table);
}
I am trying to save the contents of the text file in the data base field "data". But it is not saved. Any help would be appreciated.

File upload already exist function in codingniter

Actually, i'm trying to upload file in codigniter it's done but i want if file is already exist in uploads folder will show error message please help me.
Here my code controller:
if($this->input->post('userSubmit'))
{
//echo "comm";
if(!empty($_FILES['file_upload']['name']))
{
// echo "coming";
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['file_name'] = $_FILES['file_upload']['name'];
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_upload'))
{
//echo "cominggggggg";die;
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
//print_r($picture);die;
}
else
{
$picture = '';
}
}
else
{
$picture = '';
}
}
You don't need to initialize the CI upload settings twice and you should use ./uploads/ rather than uploads/. Otherwise it is just a matter of checking like so:
if ($this->input->post('userSubmit')) {
if (!empty($_FILES['file_upload']['name'])) {
if (is_file('./uploads/' . $_FILES['file_upload']['name'])) {
show_error('File already exists!'); // function exits
}
$config['upload_path'] = './uploads/'; // correct way
$config['allowed_types'] = 'doc|docx|pdf';
$config['file_name'] = $_FILES['file_upload']['name'];
$this->load->library('upload', $config);
//$this->upload->initialize($config); you already did this ^
if ($this->upload->do_upload('file_upload')) {
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
} else {
$picture = '';
}
} else {
$picture = '';
}
}

Can not Upload images and pdf together with codeigniter

how to upload image file and pdf data, I find it difficult because the extension in different I have made simplified but the data does not go into the folder in the show..
function add_module_external(){
$id_module = "9090909";
$file_data = array();
$image_data = array();
$config_image["file_name"] = $id_module;
$config_image["upload_path"] = "./assets/file/image-module/";
$config_image["overwrite"] = TRUE;
$config_image["allowed_types"] = "gif|jpg|png|";
$this->load->library('upload', $config_image);
$this->upload->initialize($config_image);
$upload_background = $this->upload->do_upload('other_image');
if($upload_background){
$image_data = $this->upload->data();
$image = $image_data['file_name'];
}else{
$image = $this->input->post('background');
}
// pdf and ppt upload
$config_file['file_name'] = $id_module;
$config_file['upload_path'] = '/assets/file/materi-module/';
$config_file['overwrite'] = TRUE;
$config_file['allowed_types'] = 'pdf';
$this->load->library('upload', $config_file);
$this->upload->initialize($config_file);
$upload_pdf = $this->upload->do_upload('pdf_data');
if($upload_pdf){
$file_data = $this->upload->data();
$content = $file_data["file_name"];
}else{
$content = $this->input->post("text_data");
}
$query = $this->my_module->new_module($image, $id_module, $content);
redirect($this->agent->referrer());
}
On the second time you are loading the upload library, it will be skipped because you already loaded the library, so the file config will still being loaded on the image upload.
What you could do :
1. Load the library using provided image config
2. Process image upload data
3. Initialize upload using file config
4. Process file upload data
function add_module_external(){
$id_module = "9090909";
$file_data = array();
$image_data = array();
$config_image["file_name"] = $id_module;
$config_image["upload_path"] = "./assets/file/image-module/";
$config_image["overwrite"] = TRUE;
$config_image["allowed_types"] = "gif|jpg|png|";
$this->load->library('upload', $config_image);
$upload_background = $this->upload->do_upload('other_image');
if($upload_background){
$image_data = $this->upload->data();
$image = $image_data['file_name'];
}else{
$image = $this->input->post('background');
}
// pdf and ppt upload
$config_file['file_name'] = $id_module;
$config_file['upload_path'] = '/assets/file/materi-module/';
$config_file['overwrite'] = TRUE;
$config_file['allowed_types'] = 'pdf';
$this->upload->initialize($config_file);
$upload_pdf = $this->upload->do_upload('pdf_data');
if($upload_pdf){
$file_data = $this->upload->data();
$content = $file_data["file_name"];
}else{
$content = $this->input->post("text_data");
}
$query = $this->my_module->new_module($image, $id_module, $content);
redirect($this->agent->referrer());
}
You can upload jpg and pdf files together, but you can't rename the file name
Try this code.
if(!empty($_FILES["ktp"])) {
if(!file_exists("./uploads/".$this->input->post('no_hp'))) {
mkdir("./uploads/".$this->input->post('no_hp'));
}
$configktp['upload_path'] = './uploads/'.$this->input->post('no_hp')."/";
$configktp['allowed_types'] = '*';
$this->load->library('upload', $configktp);
if ( ! $this->upload->do_upload('ktp'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
if(!empty($_FILES["ijazah"])) {
$configijazah['upload_path'] = './uploads/'.$this->input->post('no_hp')."/";
$configijazah['allowed_types'] = '*';
$this->load->library('upload', $configijazah);
if ( ! $this->upload->do_upload('ijazah'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
if(!empty($_FILES["cv"])) {
$configpdf['upload_path'] = './uploads/'.$this->input->post('no_hp')."/";
$configpdf['allowed_types'] = '*';
$this->load->library('upload', $configpdf);
if ( ! $this->upload->do_upload('cv'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}

Files not getting stored in correct folder

I have a form in CodeIgniter that allows the user to upload 2 separate files. At the backend I want these files to get stored in the different folder. In the controller i have written the upload code
public function upload()
{
/**Start uploading file**/
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
echo $error;
}
else
{
$data = $this->upload->data();
echo $file = $data['file_name']; //name of file
}
/**End uploading file**/
/**Start uploading img**/
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('img'))
{
$error1 = array('error' => $this->upload->display_errors());
echo $error1;
}
else
{
$data1 = $this->upload->data();
echo $img = $data1['file_name']; //name of img
}
/**End uploading img**/
}
The images are getting uploaded but they are getting uplaoded to same folder. Can anyone please tell how i can make the files get saved in seperate folders
in the second load , you need to initialiaze the loaded library because the load method don't initialize it :
$this->upload->initialize($config2);

upload different type files using codeigniter 3?

Is it possible to upload different files type using two input files? I created a form upload with two input and now I'm confused why the file name is same even when i uploaded different files with different name?
this is my controller :
public function file_data() {
$data['program'] = $this->input->post('program');
$this->load->library('upload');
$config['upload_path'] = './uploaded_files/laporan/absen';
$config['allowed_types'] = 'pdf';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_absen')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
unset($config);
$config['upload_path'] = './uploaded_files/laporan/nilai';
$config['allowed_types'] = 'xls|xlsx';
$config['overwrite'] = FALSE;
//$config['max_size'] = '15000';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_nilai')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
$upload_data = $this->upload->data();
//get the uploaded file name
$data['file_absen'] = $upload_data['file_name'];
$data['file_nilai'] = $upload_data['file_name'];
//store pic data to the db
$this->laporan_model->simpan_data($data);
redirect(base_url() . "tutor/laporan_np");
}
}
}
this is my model :
//fetch all data from db
function ambil_data(){
$all_files = $this->db->get('laporan');
return $files_pics->result();
}
//save datadata to db
function simpan_data($data){
$insert_data['program'] = $data['program'];
$insert_data['file_nilai'] = $data['file_nilai'];
$insert_data['file_absen'] = $data['file_absen'];
$query = $this->db->insert('laporan', $insert_data);
}
<div class="form-group">
<label for="file_nilai">File Nilai*:</label>
<input type="file" name="file_nilai" class="form-control" id="file_nilai">
</div>
<div class="form-group">
<label for="file_absen">File Absen*:</label>
<input type="file" name="file_absen" class="form-control" id="file_absen">
</div>
You could just rewrite your conditional to :
public function file_data() {
$data['program'] = $this->input->post('program');
$this->load->library('upload');
$config['upload_path'] = './uploaded_files/laporan/absen';
$config['allowed_types'] = 'pdf';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_absen')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
$upload_data_absen = $this->upload->data(); // added this..
unset($config);
$config['upload_path'] = './uploaded_files/laporan/nilai';
$config['allowed_types'] = 'xls|xlsx';
$config['overwrite'] = FALSE;
//$config['max_size'] = '15000';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_nilai')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
$upload_data_nilai = $this->upload->data(); // changed this..
//get the uploaded file name
$data['file_absen'] = $upload_data_absen['file_name']; // changed this..
$data['file_nilai'] = $upload_data_nilai['file_name']; // changed this..
//store pic data to the db
$this->laporan_model->simpan_data($data);
redirect(base_url() . "tutor/laporan_np");
}
}
}

Categories