我正在使用此代码加载数据库中的文件。这是我在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
如果名称是特定值,如何检查文件名?我想连接文件名并获取名称和日期,以便找到该文件,然后在我的代码中使用原始文件名。
最佳答案
我找到了这个问题的解决方案,但我仍然有一个与此相关的未解答的问题(链接在最后)。这个问题的答案就在这段代码中:
$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;
}
}
}
关于php - 根据文件名使用 php/sql 加载 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33507101/