【问题标题】:codeigniter issue in uploading csv file in cpanel在 cpanel 中上传 csv 文件时出现 codeigniter 问题
【发布时间】:2015-07-17 21:34:55
【问题描述】:

我收到以下错误: 不允许您尝试上传的文件类型。 我想上传 csv 文件。

我正在使用 codeigniter 文件上传方法 do_upload 我还提供 allowed_types 作为 csv

public function csvRateList(){
$redirectData=['error'=>'','success'=>''];

$type=$this->input->post('type');

date_default_timezone_set('Asia/Kolkata');

$config['upload_path'] ='./csv/';

$config['allowed_types'] = 'csv'; //type of file

$config['max_size'] = '100';

$this->load->library('upload',$config);

$query = $this->db->get_where('csv_rate_list', array('type' => $type));

    if($query->num_rows()==0){
        $query = $this->db->get_where('rate_list', array('type' => $type));
            if($query->num_rows()==0){
            if($this->upload->do_upload()){
                $fdata=$this->upload->data();
                $newName=$fdata['file_name'];
                $origName=$fdata['orig_name'];
                $data = array(
                'type'      => $type ,
                'new_name'  => $newName ,
                'orig_name' => $origName,
                'timestamp' =>time()
                );
                $this->db->insert('csv_rate_list', $data); 
            }else{
                $redirectData['error']=$this->upload->display_errors();
                redirect(base_url().'add_rate');
            }
                $redirectData['success']='Successfully inserted!';
                $this->session->set_flashdata($redirectData);
                redirect(base_url().'add_rate');
            }else{
                $redirectData['error']='Service type already exists. in old table';
                $this->session->set_flashdata($redirectData);
                redirect(base_url().'add_rate');
            }
    }else{
        $record=$query->row_array();
        $id=$record['id'];
        $old_name=$record['new_name'];
        if($this->upload->do_upload()){
            $fdata=$this->upload->data();
            $newName=$fdata['file_name'];
            $origName=$fdata['orig_name'];
            $data = array(
            'type'      => $type ,
            'new_name'  => $newName ,
            'orig_name' => $origName,
            'timestamp' =>time()
            );
            $this->db->where('id', $id);
            $this->db->update('csv_rate_list', $data); 
            unlink('./csv/'.$old_name);
            $redirectData['success']='Successfully updated!';
            $this->session->set_flashdata($redirectData);
            redirect(base_url().'add_rate');
        }else{
            $redirectData['error']=$this->upload->display_errors();
            $this->session->set_flashdata($redirectData);
            redirect(base_url().'add_rate');
        }
    }
}

【问题讨论】:

  • 让你的代码进入 if($query->num_rows()==0){ loop??
  • 检查条件 $query->num_rows() > 0
  • 这段代码在我的本地服务器 xampp 上运行良好! cpanel中文件类型“csv”只有一个问题
  • 检查上传文件夹的权限
  • 它显示的任何类型的错误??

标签: php codeigniter csv file-upload


【解决方案1】:

您必须修改 (/system/libraries/Upload.php) 中的几行

来自:

$this->file_type = @mime_content_type($file['tmp_name']);
return;

到这里:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 

【讨论】:

    【解决方案2】:

    检查 config/mimes.php 的“csv”值是否正确

    在 $mimes 数组中,找到 'csv' 并检查以下内容。

    'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),

    如果没有,请将其添加到其中。

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 2012-01-09
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多