马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
记载:fastadmin 表格数据导入
php代码
在Backend.php文件中增长读取文件数据并返回代码
- /**
- * 读取文件数据并返回
- * @return array
- */
- protected function readFile($file)
- {
-
-
- if (!$file) {
-
-
- $this->error(__('Parameter %s can not be empty', 'file'));
- }
-
- $filePath = ROOT_PATH . DS . 'public' . DS . $file;
- // //获取上传文件后缀
- // $temp = explode("/",$file);
- // $filename = end($temp);
- // $filePath = ROOT_PATH . "public" .DS. "uploads" .DS. date('Ymd').DS.$filename;
- if (!is_file($filePath)) {
-
-
- $this->error(__('No results were found'));
- }
- //实例化reader
- $ext = pathinfo($filePath, PATHINFO_EXTENSION);
- if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
-
-
- $this->error(__('Unknown data format'));
- }
-
- if ($ext === 'csv') {
-
-
- $file = fopen($filePath, 'r');
- $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
- $fp = fopen($filePath, "w");
- $n = 0;
- while ($line = fgets($file)) {
-
-
- $line = rtrim($line, "\n\r\0");
- $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
- if ($encoding != 'utf-8') {
-
-
- $line = mb_convert_encoding($line, 'utf-8', $encoding);
- }
- if ($n == 0 || preg_match('/^".*"$/', $line)) {
-
-
- fwrite($fp, $line . "\n");
- } else {
-
-
- fwrite($fp,
复制代码 |