在 Codeigniter 中上传 CSV 文件时出错
Posted
技术标签:
【中文标题】在 Codeigniter 中上传 CSV 文件时出错【英文标题】:Error on uploading CSV File in Codeigniter 【发布时间】:2015-12-10 05:42:10 【问题描述】:您好,我正在尝试使用 CI 库将 CSV 导入 mysql 数据库。但我在 Controller 中收到此错误,即现在允许文件类型
控制器:
<?php
class Csv extends CI_Controller
function __construct()
parent::__construct();
$this->load->model('csv_model');
$this->load->library('csvimport');
function index()
$data['addressbook'] = $this->csv_model->get_addressbook();
$this->load->view('csvindex', $data);
function importcsv()
$data['addressbook'] = $this->csv_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(
'firstname'=>$row['firstname'],
'lastname'=>$row['lastname'],
'phone'=>$row['phone'],
'email'=>$row['email'],
);
$this->csv_model->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('csvindex', $data);
/*END OF FILE*/
模型:
<?php
class Csv_model extends CI_Model
function __construct()
parent::__construct();
function get_addressbook()
$query = $this->db->get('addressbook');
if ($query->num_rows() > 0)
return $query->result_array();
else
return FALSE;
function insert_csv($data)
$this->db->insert('addressbook', $data);
/*END OF FILE*/
我正在尝试使用 PHP Codeigniter 导入 CSV。现在我收到一个错误,
您尝试上传的文件类型不允许。
如您所见,我保留了允许的文件类型 = csv,那么这个问题也即将到来。所以请帮忙。谢谢
【问题讨论】:
config/mimes.php 你应该在 csv 键中添加text/plain
@Abdulla 什么类型的密钥?
使用 csc 搜索。可能是文件顶部
不明白你的意思@Abdulla
转到文件并在 config/mimes.php 文件中搜索 csv 并在行尾添加 text/plain
【参考方案1】:
您可以使用 CALLBACK 函数来代替更改 mime 类型 这也将增加更多的可移植性...
function importcsv()
$data['addressbook'] = $this->csv_model->get_addressbook();
$data['error'] = ''; //initialize image upload error array to empty
$this->form_validation->set_rules('uploaded_file','Uploaded file', 'trim|callback_chk_attachment');
if($this->form_validation->run())
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$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(
'firstname'=>$row['firstname'],
'lastname'=>$row['lastname'],
'phone'=>$row['phone'],
'email'=>$row['email'],
);
$this->csv_model->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('csvindex', $data);
Call Back function for File Upload validation:
public function chk_attachment() // callback validation for check the attachment extension
$file_type = array('.csv');
if(!empty($_FILES['uploaded_file']['name']))
$ext = strtolower(strrchr($_FILES['uploaded_file']['name'],"."));
if(in_array($ext,$ext_array))
return true;
else
$this->form_validation->set_message('chk_attachment','Attachment allowed only csv');
return false;
$this->form_validation->set_message('chk_attachment','image field is required');
return false;
【讨论】:
仍然收到错误您尝试上传的文件类型不被允许。 我也有这个问题。但是这个回电修复了我!无论如何,我会找到其他解决方案。 我只是使用了你的代码@blacmoon,但没有显示任何差异:(以上是关于在 Codeigniter 中上传 CSV 文件时出错的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 PHP CodeIgniter 将 CSV 数据导入 MYSQL 数据库?
Codeigniter 将部分 CSV 数据插入到 MYSQL 中,只有 id 和 date