when I run it on localhost my csv uploading file is completely fine but when I host it in the web it will get some error. Its redirecting me to the csvindex.php I already check the columns in csv file
Here's my importcsv():
function importcsv($org, $course) {
$data['addressbook'] = $this->users_model->get_addressbook();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row) {
$insert_data = array(
'student_fname'=>$row['student_fname'],
'student_lname'=>$row['student_lname'],
'student_course'=>$row['student_course'],
'email'=>$row['email'],
'student_dept'=>$row['student_dept'],
'student_year'=>$row['student_year'],
'student_img'=>$row['student_img'],
'contact'=>$row['contact'],
'password'=>md5($row['password'])
);
$this->users_model->insert_csv($insert_data);
}
redirect('admin/colleges/show_students/'.$org.'/'.$course);
} else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
Here's my get_addressbook():
function get_addressbook() {
$query = $this->db->get('tbl_students');
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
Can someone help me with this please?
Check $file_path,the upload folder path in your server and check your upload folder permission – Sharmistha Das 2 hours ago
The error seems to be that your upload path either does not exist or does not have the right permissions.
Best way to solve it is to go in via FTP or SSH to create the "uploads" folder in your codeigniter root.
Related
i wants to import csv in PHP codeigniter.am getting error.
this is my controller
function importcsv() {
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/csv/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('cforce/enquiryform');
} else {
$file_data = $this->upload->data();
$file_path = './uploads/csv/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
var_dump($csv_array);die;
foreach ($csv_array as $row) {
$insert_data = array(
'name'=>$row['name'],
'subject'=>$row['subject'],
'mark'=>$row['mark'],
);
$this->Adminpanelmodel->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('cforce/enquiryform');
}
}
either you forget to load csvimport library, or you should check in your controller "cforce" that is there any file named "enquiryform". Means you should inspect this line - $this->load->view('cforce/enquiryform');
If this not work, make your controller's first letter in uppercase.May be it'll work for you.
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);
Which file permission should I use for uploading and downloading files to a folder in codeigniter?
My whole project is been hosted on FileZilla.
NOTE: Uploading downloading works perfectly fine when website is hosted loacally.
Error only occurs when hosted through FileZilla.
My Upload/Download folder is present in root directory of codeigniter (Directory where Application folder is present).
Upload Controller Code
public function do_upload(){
$rti_details['rtino'] = $this->input->post("rtino");
$rtino_result = $this->rti_model->get_rti_details_by_rtino($rti_details['rtino']);
if(!$rtino_result){
$upload=$this->upload_file('rtifile',$this->input->post('rtino'));
if($upload)
{
$data = array('rti_no'=>$this->input->post('rtino'),
'filer_name'=>$this->input->post('filername'),
'filer_add'=>$this->input->post('fileradd'),
'city'=>$this->input->post('city'),
'state'=>$this->input->post('state'),
'pin_code'=>$this->input->post('pin_code'),
'rti_cat'=>$this->input->post('rti_cat'),
'rti_file'=>$upload['full_path'],
'filed_on'=>$this->input->post('filedon')
);
$this->rti_model->insert_rti($data);
}
$result = true;
}
else
$result = false;
if($result)
$this->session->set_flashdata("flashSuccess","RTI added successfully");
else
$this->session->set_flashdata("flashError","Error in adding RTI. This RTI Number Already Exist.");
redirect("rti/rti_file");
}
private function upload_file($name ='',$sno = 0)
{
if($name=='rtifile'){ $config['upload_path'] = 'assets/rti_uploads/rti_file/'; }
if($name=='coverletter'){ $config['upload_path'] = 'assets/rti_uploads/cover_letter/'; }
if($name=='fullreply'){ $config['upload_path'] = 'assets/rti_uploads/full_reply/'; }
$config['allowed_types'] = 'pdf';
$config['max_size'] = '2050';
if(isset($_FILES[$name]['name']))
{
if($_FILES[$name]['name'] == "")
$filename = "";
else
{
$filename=$this->security->sanitize_filename(strtolower($_FILES[$name]['name']));
$ext = strrchr( $filename, '.' );
if($name=='rtifile'){ $filename='RTI_'.$sno.'_'.date('YmdHis').$ext; }
if($name=='coverletter'){ $filename='COVER_'.$sno.'_'.date('YmdHis').$ext; }
if($name=='fullreply'){ $filename='FULLREPLY_'.$sno.'_'.date('YmdHis').$ext; }
}
}
else
{
$this->session->set_flashdata('flashError','ERROR: File Name not set.');
redirect('rti/rti_file');
return FALSE;
}
$config['file_name'] = $filename;
if(!is_dir($config['upload_path']))
{
mkdir($config['upload_path'],0777,TRUE);
}
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($name))
{
$this->session->set_flashdata('flashError',$this->upload->display_errors('',''));
redirect('rti/rti_file');
return FALSE;
}
else
{
$upload_data = $this->upload->data();
return $upload_data;
}
}
From the documentation:
You’ll need a destination directory for your uploaded images. Create a
directory at the root of your CodeIgniter installation called uploads
and set its file permissions to 777.
http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-upload-directory
Best wishes,
Paul
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
While uploading csv file iam saving that file in uploads folder if the file uplaoded successfully into database or not uploaded then also it should be deleted automatically from that folder.Can any one help me regarding this.
$data['error'] = '';
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '10000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
}
else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
$csv_array = $this->csvimport->get_array($file_path,'',FALSE,0,3,0,$cformat);
if ($csv_array) {
$successflag=true;
foreach ($csv_array as $row) {
$order = array(
'department'=>$row['Department'],
'gender'=>$row['Gender'],
);
$query = $this->db->query("select count(*) cnt from order_master where order_id='{$order['order_id']}' ");
$row = $query->first_row();
if(trim($order['order_id'] )!="" && $row->cnt==0 ) {
$this->masterorder_model->order($order);
}
else if ( $row->cnt>0) {
$successflag=false;
$this->flash->success("<h5><font color='red'>Found Duplicate Order Id'{$order['order_id']}' for order name '$oname'</font></h5>");
break;
}
}
if(!$successflag) {
$this->db->trans_rollback();
}
else {
$this->db->trans_commit();
$this->flash->success('<h5>Csv Data Imported Successfully.</h5>');
}
redirect(base_url().'masterorder/index');
}
else {
$this->flash->success('<h5><font color="red">Invalid file format.</font></h5>');
redirect(base_url().'masterorder/index');
}
}
}
Use unlink() to delete file after processing.
Deletes filename. Similar to the Unix C unlink() function. A E_WARNING
level error will be generated on failure.
you can use the file helper for the codeigniter.
So it would be like as below :
$this->load->helper("file");
delete_files($path);
Please visit this for more information.
You can use unlink('path/filename' )