从目录转储结构导入产品


Importing products from directory dump structure

我的数据库转储的目录结构如下(总共:18042482个产品):

/home/nataliya/dump/10xxxxx/100xxxx/1000006/
/home/nataliya/dump/10xxxxx/100xxxx/1000007/
...

其中10000061000007是产品的id

每个文件夹包含:

description.txt (text)
details.csv (name, oldprice, price)
images.csv (url, size)

如何使用PHP将它们导入MySQL,如下所示:

id, name, oldprice, price, description (table `products`)
product_id, url, size (table `images`)

如果问题与本网站无关,我提前道歉,并感谢所有希望帮助我的人。


编辑:为了澄清,我只要求提供指导,而不是完整的代码示例。只是PHP的适当功能,这将使它具有良好的性能,因为我说的是一个包含3000多万个文件的目录。


编辑:以下是我目前所做的:

<?php
function ArrayCSV($file) {
    $fh = fopen($file, 'r');
    while (!feof($fh) ) {
        $result[] = fgetcsv($fh, 1024);
    }
    fclose($fh);
    return $result[1];
}
$products = array();
$images = array();
foreach(scandir(dirname(__FILE__) . '/dump/') as $dir_global) {
    if($dir_global != '.' && $dir_global != '..' && $dir_global != '.DS_Store') {
        foreach(scandir(dirname(__FILE__) . '/dump/' . $dir_global) as $dir_local) {
            if($dir_local != '.' && $dir_local != '..' && $dir_local != '.DS_Store') {
                foreach(scandir(dirname(__FILE__) . '/dump/' . $dir_global . '/' . $dir_local) as $id) {
                    if($id != '.' && $id != '..' && $id != '.DS_Store') {
                        $dir = dirname(__FILE__) . '/dump/' . $dir_global . '/' . $dir_local . '/' . $id;
                        $product = ArrayCSV($dir . '/details.csv');
                        $image = ArrayCSV($dir . '/images.csv');
                        $products[] = array(
                            'id' => $id,
                            'name' => $product[0],
                            'oldprice' => $product[1],
                            'price' => $product[2],
                            'description' => file_get_contents($dir . '/description.txt')
                        );
                        $images[] = array(
                            'product_id' => $id,
                            'url' => $image[0],
                            'size' => $image[1]
                        );
                    }
                }
            }
        }
    }
}
try {
    $DBH = new PDO("mysql:host=localhost;dbname=application", 'root', '');
    $DBH->exec("SET NAMES utf8");
}
catch(PDOException $e) {
    //
}
foreach($products as $product) {
    $STH = $DBH->prepare("INSERT INTO products (id, name, oldprice, price, description) VALUES (:id, :name, :oldprice, :price, :description)");
    $STH->execute($product);
}
foreach($images as $image) {
    $STH = $DBH->prepare("INSERT INTO images (product_id, url, size) VALUES (:product_id, :url, :size)");
    $STH->execute($image);
}
?>

我建议您熟悉以下函数scandir()file_get_contents(),最后是PHP的所有mysqli函数,因为它们是您需要在这里做什么的关键。

希望这能有所帮助!