PHP - 在自定义 Wordpress 数据库中上传 CSV 数据
PHP - Upload CSV data in custom Wordpress database
my_custom_table:
id int(11)
product_type varchar(210)
product_serial varchar(210)
created_at datetime
//Upload CSV File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['upload_csv']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['upload_csv']['name'] ." uploaded successfully." . "</h1>";
}
//Import uploaded file to Database
$handle = fopen($_FILES['upload_csv']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$wpdb->insert("my_custom_table", array(
"product_type" => $data[0],
"product_serial" => $data[1],
"created_at" => current_time('mysql', 1)
));
}
fclose($handle);
print "Import done";
}
打印 $data
带分隔符的输出 ,
:
Array
(
[0] => Product Name;product_serial
)
Array
(
[0] => iPhone 6;iphone-002
)
Array
(
[0] => iPhone 6;iphone-003
)
打印 $data
带分隔符的输出 ;
:
Array
(
[0] => Product Name
[1] => product_serial
)
Array
(
[0] => iPhone 6
[1] => iphone-002
)
Array
(
[0] => iPhone 6
[1] => iphone-003
)
使用上面的方法,Product Name
和 product_serial
也被插入到应该被阻止的数据库中。此外,定界符 ,
不会输出正确的数组,而 ;
会。
如何防止插入 CSV 列名并在数据库中插入正确的值?
P.S: 使用 OpenOffice 进行 CSV 数据插入。格式可能是分隔符的问题吗?
一般的经验法则是 CSV 的第一行是列名,因此快速跳过计数器将为您删除第一行:
$counter = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Skip the first row as is likely column names
if ($counter === 0) {
$counter++;
continue;
}
// Insert the row into the database
$wpdb->insert("my_custom_table", array(
"product_type" => $data[0],
"product_serial" => $data[1],
"created_at" => current_time('mysql', 1)
));
}
另一个问题是 CSV 文件可以有不同的列和行分隔符(也就是有时列由逗号分隔,有时由分号分隔......等等)使得解析 CSV 非常困难。在这个例子中,您的列分隔符似乎是分号,因此修改您的函数参数可能会为您修复它:
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
如果您确实必须支持多种类型的定界符,以下代码片段可能对您有所帮助:
$csvFilePath = $_FILES['upload_csv']['tmp_name'];
$delimiter = $this->detectDelimiter($csvFilePath);
public function detectDelimiter($csvFile)
{
$delimiters = array(
';' => 0,
',' => 0,
"\t" => 0,
"|" => 0
);
$handle = fopen($csvFile, "r");
$firstLine = fgets($handle);
fclose($handle);
foreach ($delimiters as $delimiter => &$count) {
$count = count(str_getcsv($firstLine, $delimiter));
}
return array_search(max($delimiters), $delimiters);
}
检测摘自的片段:HERE
希望这对您有所帮助。
my_custom_table:
id int(11)
product_type varchar(210)
product_serial varchar(210)
created_at datetime
//Upload CSV File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['upload_csv']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['upload_csv']['name'] ." uploaded successfully." . "</h1>";
}
//Import uploaded file to Database
$handle = fopen($_FILES['upload_csv']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$wpdb->insert("my_custom_table", array(
"product_type" => $data[0],
"product_serial" => $data[1],
"created_at" => current_time('mysql', 1)
));
}
fclose($handle);
print "Import done";
}
打印 $data
带分隔符的输出 ,
:
Array
(
[0] => Product Name;product_serial
)
Array
(
[0] => iPhone 6;iphone-002
)
Array
(
[0] => iPhone 6;iphone-003
)
打印 $data
带分隔符的输出 ;
:
Array
(
[0] => Product Name
[1] => product_serial
)
Array
(
[0] => iPhone 6
[1] => iphone-002
)
Array
(
[0] => iPhone 6
[1] => iphone-003
)
使用上面的方法,Product Name
和 product_serial
也被插入到应该被阻止的数据库中。此外,定界符 ,
不会输出正确的数组,而 ;
会。
如何防止插入 CSV 列名并在数据库中插入正确的值?
P.S: 使用 OpenOffice 进行 CSV 数据插入。格式可能是分隔符的问题吗?
一般的经验法则是 CSV 的第一行是列名,因此快速跳过计数器将为您删除第一行:
$counter = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Skip the first row as is likely column names
if ($counter === 0) {
$counter++;
continue;
}
// Insert the row into the database
$wpdb->insert("my_custom_table", array(
"product_type" => $data[0],
"product_serial" => $data[1],
"created_at" => current_time('mysql', 1)
));
}
另一个问题是 CSV 文件可以有不同的列和行分隔符(也就是有时列由逗号分隔,有时由分号分隔......等等)使得解析 CSV 非常困难。在这个例子中,您的列分隔符似乎是分号,因此修改您的函数参数可能会为您修复它:
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
如果您确实必须支持多种类型的定界符,以下代码片段可能对您有所帮助:
$csvFilePath = $_FILES['upload_csv']['tmp_name'];
$delimiter = $this->detectDelimiter($csvFilePath);
public function detectDelimiter($csvFile)
{
$delimiters = array(
';' => 0,
',' => 0,
"\t" => 0,
"|" => 0
);
$handle = fopen($csvFile, "r");
$firstLine = fgets($handle);
fclose($handle);
foreach ($delimiters as $delimiter => &$count) {
$count = count(str_getcsv($firstLine, $delimiter));
}
return array_search(max($delimiters), $delimiters);
}
检测摘自的片段:HERE
希望这对您有所帮助。