dtmu88600 2015-09-14 09:14
浏览 47
已采纳

在Codeigniter中上传CSV文件时出错

Hi I am trying to import an CSV into MYSQL database using A CI Library. But I get this error in Controller that file type is now allowed

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*/

The Model :

    <?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*/

I am trying to Import A CSV using PHP Codeigniter. Now I am getting an error that

The filetype you are attempting to upload is not allowed.

So as you can see i have kept the allowed file type = csv then also this issue is coming. So please help. Thanks

  • 写回答

1条回答 默认 最新

  • doqw89029 2015-09-14 11:25
    关注

    Instead of changing the mime type you can go with the CALLBACK function This will add more portability too...

     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;
                }
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿