无法上传csv文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法上传csv文件相关的知识,希望对你有一定的参考价值。

我正在尝试上传一个csv文件,这是一个Microsoft excel类型的文件,似乎导入csv文件时出现问题。

控制器(Csv_import.php):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Csv_import extends CI_Controller {

 public function __construct()
 {
  parent::__construct();
  $this->load->model('csv_import_model');
  $this->load->library('csvimport');
 }

  function index()
    {
      $this->load->view('csv_import');
      $this->load_data();
    }

  function load_data()
    {
      $result = $this->csv_import_model->select();
      $output = '
       <h3 align="center">Imported User Details from CSV File</h3>
       <div class="table-responsive">
         <table class="table table-bordered table-striped">
           <tr>
            <th>ID Number</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Gender</th>
            <th>Course</th>
            <th>Year</th>
          </tr>
              ';
      $count = 0;
      if($result->num_rows() > 0)
       {
        foreach($result->result() as $row)
         {
          $count = $count + 1;
          $output .= '
          <tr>
           <td>'.$count.'</td>
           <td>'.$row->fname.'</td>
           <td>'.$row->lname.'</td>
           <td>'.$row->gndr.'</td>
           <td>'.$row->crse.'</td>
           <td>'.$row->yr.'</td>
          </tr>
          ';
          }
         }
        else
         {
          $output .= '
           <tr>
           <td colspan="6" align="center">Data not Available</td>
           </tr>
          ';
    }
   $output .= '</table></div>';
   echo $output;
    }

   function import()
    {
     $file_data = $this->csvimport->get_array($_FILES["csv_file"] 
     ["tmp_name"]);
     foreach($file_data as $row)
      {
       $data[] = array(
      'id_number' => $row["ID Number"],
      'fname'  => $row["First Name"],
      'lname'  => $row["Last Name"],
      'gndr'   => $row["Gender"],
      'crse'   => $row["Course"],
      'yr'   => $row["Year"]
     );
    }
   $this->csv_import_model->insert($data);
  }


}

模型(csv_import_model.php):

<?php
 class Csv_import_model extends CI_Model
  {
   function select()
    {
      $this->db->order_by('id_number', 'DESC');
      $query = $this->db->get('tbl_user');
      return $query;
  }

   function insert($data)
    {
     $this->db->insert_batch('tbl_user', $data);
    }
  }

和视图(csv_import.php):

<html>
<head>
<title>CSV Sample</title>

<script type="text/javascript" src="<?php echo base_url();
>bootstrap/js/jquery.js" ></script>
<script type="text/javascript" src="<?php echo base_url();
>bootstrap/dist/js/bootstrap.js" ></script>
<link href="<?php echo base_url()?>bootstrap/dist/css/bootstrap.css" 
rel="stylesheet" type="text/css"/>

</head>
<body>
<div class="container box">
<h3 align="center">CSV Sample</h3>
<br />

<form method="post" id="import_csv" enctype="multipart/form-data">
 <div class="form-group">
  <label>Select CSV File</label>
  <input type="file" name="csv_file" id="csv_file" required accept=".csv" />
 </div>
 <br />
 <button type="submit" name="import_csv" class="btn btn-info" 
  id="import_csv_btn">Import CSV</button>
 </form>
 <br />
  <div id="imported_csv_data"></div>
  </div>
  </body>
  </html>
  <script>

$(document).ready(function(){

 load_data();

function load_data()
 {
  $.ajax({
  url:"<?php echo base_url(); ?>csv_import/load_data",
  method:"POST",
  success:function(data)
   {
    $('#imported_csv_data').html(data);
   }
   })
   }

 $('#import_csv').on('submit', function(event){
  event.preventDefault();
  $.ajax({
  url:"<?php echo base_url(); ?>csv_import/import",
  method:"POST",
  data:new FormData(this),
  contentType:false,
  cache:false,
  processData:false,
  beforeSend:function(){
 $('#import_csv_btn').html('Importing...');
  },
 success:function(data)
  {
   load_data();
   $('#import_csv')[0].reset();
   $('#import_csv_btn').attr('disabled', false);
   $('#import_csv_btn').html('Import Done');

    }
  })
 });

 });
</script>

当我点击导入按钮没有任何反应时,我还在运行代码之前下载了csv库。顺便说一句,我正在使用CodeIgniter

答案

大声笑我知道这是一年,但不知怎的,我设法回来解决这个问题。我认为我自己的问题是我从其他来源复制了这段代码,并保证这段代码100%正常工作,所以为了让这段代码能够正常工作,我编辑了控制器,而不是这样:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Csv_import extends CI_Controller {

 public function __construct()
  {
  parent::__construct();
  $this->load->model('csv_import_model');
  $this->load->library('csvimport');
 }

  function index()
    {
      $this->load->view('csv_import');
      $this->load_data();
    }

  function load_data()
    {
      $result = $this->csv_import_model->select();
      $output = '
       <h3 align="center">Imported User Details from CSV File</h3>
       <div class="table-responsive">
       <table class="table table-bordered table-striped">
       <tr>
        <th>ID Number</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Gender</th>
        <th>Course</th>
        <th>Year</th>
      </tr>
          ';
      $count = 0;
      if($result->num_rows() > 0)
       {
        foreach($result->result() as $row)
         {
          $count = $count + 1;
          $output .= '
          <tr>
           <td>'.$count.'</td>
           <td>'.$row->fname.'</td>
           <td>'.$row->lname.'</td>
           <td>'.$row->gndr.'</td>
           <td>'.$row->crse.'</td>
           <td>'.$row->yr.'</td>
          </tr>
          ';
          }
         }
    else
     {
      $output .= '
       <tr>
       <td colspan="6" align="center">Data not Available</td>
       </tr>
      ';
    }
   $output .= '</table></div>';
   echo $output;
    }

   function import()
    {
     $file_data = $this->csvimport->get_array($_FILES["csv_file"] 
     ["tmp_name"]);
     foreach($file_data as $row)
       {
       $data[] = array(
      'id_number' => $row["ID Number"],
      'fname'  => $row["First Name"],
      'lname'  => $row["Last Name"],
      'gndr'   => $row["Gender"],
      'crse'   => $row["Course"],
      'yr'   => $row["Year"]
     );
    }
   $this->csv_import_model->insert($data);
  }


    }

我将$ output替换为echo然后它应该是这样的:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Csv_import extends CI_Controller {

 public function __construct()
  {
  parent::__construct();
  $this->load->model('csv_import_model');
  $this->load->library('csvimport');
 }

  function index()
    {
      $this->load->view('csv_import');
      $this->load_data();
    }

  function load_data()
    {
      $result = $this->csv_import_model->select();
      $echo'
       <h3 align="center">Imported User Details from CSV File</h3>
       <div class="table-responsive">
       <table class="table table-bordered table-striped">
       <tr>
        <th>ID Number</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Gender</th>
        <th>Course</th>
        <th>Year</th>
      </tr>
          ';
      $count = 0;
      if($result->num_rows() > 0)
       {
        foreach($result->result() as $row)
         {
          $count = $count + 1;
          $echo '
          <tr>
           <td>'.$count.'</td>
           <td>'.$row->fname.'</td>
           <td>'.$row->lname.'</td>
           <td>'.$row->gndr.'</td>
           <td>'.$row->crse.'</td>
           <td>'.$row->yr.'</td>
          </tr>
          ';
          }
         }
    else
     {
      $echo'
       <tr>
       <td colspan="6" align="center">Data not Available</td>
       </tr>
      ';
    }
   $echo '</table></div>';
    }

   function import()
    {
     $file_data = $this->csvimport->get_array($_FILES["csv_file"] 
     ["tmp_name"]);
     foreach($file_data as $row)
       {
       $data[] = array(
      'id_number' => $row["ID Number"],
      'fname'  => $row["First Name"],
      'lname'  => $row["Last Name"],
      'gndr'   => $row["Gender"],
      'crse'   => $row["Course"],
      'yr'   => $row["Year"]
     );
    }
   $this->csv_import_model->insert($data);
  }


    }

它终于奏效了。

以上是关于无法上传csv文件的主要内容,如果未能解决你的问题,请参考以下文章

无法上传csv文件

Chrome 文件上传无法识别接受 mimetype 'text/csv'

nzSQLException 读取超时错误

无法上传 csv 文件并出现错误 C:\xampp\tmp\php9F4F.tmp

无法将带有塞浦路斯数据的csv文件上传到Google数据工作室-编码utf-8错误

无法批量加载,因为无法打开文件“ File.csv”。操作系统错误代码5(访问被拒绝。)