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' )
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.
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 = '';
}
}
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
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.
I'm adding onto an admin CMS, and need image files uploaded to save to a separate folder I have specified. I can save them to a predefined folder of another controller, but how do I save them to the new folder I have created?
When I try, it doesn't allow it. I know it has something to do with routing, but where to I change codeigniter to allow that? I have looked in config/routes.php and don't see it listed there.
Thank you!
you can try this
function uploadFile($uploadFile,$filetype,$folder,$fileName='')
{
$resultArr = array();
$config['max_size'] = '1024000';
if($filetype == 'img') $config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['upload_path'] = './uploads/'.$folder.'/';
if($fileName != "")
$config['file_name'] = $fileName;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload($uploadFile))
{
$resultArr['success'] = false;
$resultArr['error'] = $this->upload->display_errors();
} else {
$resArr = $this->upload->data();
$resultArr['success'] = true;
$resultArr['path'] = "uploads/".$folder."/".$resArr['file_name'];
}
return $resultArr;
}