Php Can';不要上传csv文件

Php Can';不要上传csv文件,php,mysql,ajax,codeigniter,Php,Mysql,Ajax,Codeigniter,我正在尝试上载一个csv文件,它是Microsoft excel类型的文件,导入csv文件时似乎有问题 控制器(Csv_import.php): 哈哈,我知道已经一年了,但不知怎么的,我设法回来解决了这个问题。我认为我自己的问题是,我从另一个来源复制了这段代码,并保证这段代码100%正常工作,因此为了让这段代码正常工作,我编辑了控制器,而不是: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed')

我正在尝试上载一个csv文件,它是Microsoft excel类型的文件,导入csv文件时似乎有问题

控制器(Csv_import.php):


哈哈,我知道已经一年了,但不知怎么的,我设法回来解决了这个问题。我认为我自己的问题是,我从另一个来源复制了这段代码,并保证这段代码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);
  }


    }

这是为Laravel设计的,但很容易实现。为了获得灵感,您是否尝试过存储文件,然后对其进行解析?您正在从临时目录抓取文件
<?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);
    }
  }
<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>
<?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);
  }


    }
<?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);
  }


    }