fastadmin 表格数据导入

[复制链接]
发表于 2025-11-15 03:56:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
记载:fastadmin 表格数据导入
php代码
在Backend.php文件中增长读取文件数据并返回代码
  1.     /**
  2.      * 读取文件数据并返回
  3.      * @return array
  4.      */
  5.     protected function readFile($file)
  6.     {
  7.    
  8.            
  9.         if (!$file) {
  10.    
  11.    
  12.             $this->error(__('Parameter %s can not be empty', 'file'));
  13.         }
  14.         
  15.         $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  16.         // //获取上传文件后缀
  17.         // $temp = explode("/",$file);
  18.         // $filename = end($temp);
  19.         // $filePath = ROOT_PATH . "public" .DS. "uploads" .DS. date('Ymd').DS.$filename;
  20.         if (!is_file($filePath)) {
  21.    
  22.    
  23.             $this->error(__('No results were found'));
  24.         }
  25.         //实例化reader
  26.         $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  27.         if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  28.    
  29.    
  30.             $this->error(__('Unknown data format'));
  31.         }
  32.         if ($ext === 'csv') {
  33.    
  34.    
  35.             $file = fopen($filePath, 'r');
  36.             $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  37.             $fp = fopen($filePath, "w");
  38.             $n = 0;
  39.             while ($line = fgets($file)) {
  40.    
  41.    
  42.                 $line = rtrim($line, "\n\r\0");
  43.                 $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  44.                 if ($encoding != 'utf-8') {
  45.    
  46.    
  47.                     $line = mb_convert_encoding($line, 'utf-8', $encoding);
  48.                 }
  49.                 if ($n == 0 || preg_match('/^".*"$/', $line)) {
  50.    
  51.    
  52.                     fwrite($fp, $line . "\n");
  53.                 } else {
  54.    
  55.    
  56.                     fwrite($fp,
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表