根据文件名使用 php/sql 加载 excel 文件
Load excel files using php/sql depending on filename
我正在使用这段代码在我的数据库中加载一个文件。这是我上传到 putty 的 php 文件:
$Db->query('LOAD DATA LOCAL INFILE \'/name 03.11.2015.csv\'
INTO TABLE '.$in_table.'
FIELDS TERMINATED BY \',\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 8 ROWS
(@date, number, @name)
set date=str_to_date(@date,\'%Y-%m-%d\'),
name= \'name\'
;');
现在我想在查询的第一行有一个变量而不是特定的文件名:
$Db->query('LOAD DATA LOCAL INFILE \'/path/name 03.11.2015.csv\'
所以,我正在使用它,它似乎在工作:
$date = '.....';
$name = '.....';
$Db->query('LOAD DATA INFILE \'/path/'.$name.' '.$date.'.csv\'
INTO TABLE '.$in_table.'
FIELDS TERMINATED BY \',\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 8 ROWS
(@date, number, @name)
set date=str_to_date(@date,\'%Y-%m-%d\'),
name= \'name\'
;');
但现在我必须更改我的代码,因为某些文件的文件名中没有这种格式:'$name $date'
。下面给出了一些示例:
- 姓名 2015-10-10
- 随机名称 2015-10-10
如果名称是特定值,我如何检查文件名?我想连接文件名并获取名称和日期以便找到文件,然后在我的代码中使用原始文件名。
我找到了这个问题的解决方案,但我仍然有一个与此问题相关的未解决方案(最后 link)。这个问题的答案在这段代码中:
$searchString = 'aaaa';
$Dates = array();
// Get all the files in my folder with the extension ".xlsx"
$files = glob('/path/*.xlsx');
// I create an array where I save all the .xlsx files that contain "aaaa" in the filename
$filesFound = array();
foreach($files as $file) {
$name = pathinfo($file, PATHINFO_FILENAME);
// Determines if there is a date and if the search string is in the filename. If yes, it puts the date and the filename in the arrays I created before
if((strpos(strtolower($name),strtolower($searchString))) && (preg_match('~(\d{2}\.\d{2}\.\d{4})~', $name, $matches))) {
$filesFound[] = $name;
$Dates[] = $matches[1];
foreach($filesFound as $ftbu){
$sql = 'LOAD DATA LOCAL INFILE \'/path/'.$ftbu.'.xlsx\' INTO TABLE '.$dbtable.'
FIELDS TERMINATED BY \',\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
(@date, number, @name)
set date=str_to_date(@date,\'%Y-%m-%d\'),
name = \'AAA\'
';
$Db->query($sql);
echo $Db->error;
}
}
}
Link:
Unanswered Question
我正在使用这段代码在我的数据库中加载一个文件。这是我上传到 putty 的 php 文件:
$Db->query('LOAD DATA LOCAL INFILE \'/name 03.11.2015.csv\'
INTO TABLE '.$in_table.'
FIELDS TERMINATED BY \',\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 8 ROWS
(@date, number, @name)
set date=str_to_date(@date,\'%Y-%m-%d\'),
name= \'name\'
;');
现在我想在查询的第一行有一个变量而不是特定的文件名:
$Db->query('LOAD DATA LOCAL INFILE \'/path/name 03.11.2015.csv\'
所以,我正在使用它,它似乎在工作:
$date = '.....';
$name = '.....';
$Db->query('LOAD DATA INFILE \'/path/'.$name.' '.$date.'.csv\'
INTO TABLE '.$in_table.'
FIELDS TERMINATED BY \',\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 8 ROWS
(@date, number, @name)
set date=str_to_date(@date,\'%Y-%m-%d\'),
name= \'name\'
;');
但现在我必须更改我的代码,因为某些文件的文件名中没有这种格式:'$name $date'
。下面给出了一些示例:
- 姓名 2015-10-10
- 随机名称 2015-10-10
如果名称是特定值,我如何检查文件名?我想连接文件名并获取名称和日期以便找到文件,然后在我的代码中使用原始文件名。
我找到了这个问题的解决方案,但我仍然有一个与此问题相关的未解决方案(最后 link)。这个问题的答案在这段代码中:
$searchString = 'aaaa';
$Dates = array();
// Get all the files in my folder with the extension ".xlsx"
$files = glob('/path/*.xlsx');
// I create an array where I save all the .xlsx files that contain "aaaa" in the filename
$filesFound = array();
foreach($files as $file) {
$name = pathinfo($file, PATHINFO_FILENAME);
// Determines if there is a date and if the search string is in the filename. If yes, it puts the date and the filename in the arrays I created before
if((strpos(strtolower($name),strtolower($searchString))) && (preg_match('~(\d{2}\.\d{2}\.\d{4})~', $name, $matches))) {
$filesFound[] = $name;
$Dates[] = $matches[1];
foreach($filesFound as $ftbu){
$sql = 'LOAD DATA LOCAL INFILE \'/path/'.$ftbu.'.xlsx\' INTO TABLE '.$dbtable.'
FIELDS TERMINATED BY \',\'
ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
(@date, number, @name)
set date=str_to_date(@date,\'%Y-%m-%d\'),
name = \'AAA\'
';
$Db->query($sql);
echo $Db->error;
}
}
}
Link: Unanswered Question