【问题标题】:Read A Folder And Split Sub-Folder Names Into MySQL Inserts读取文件夹并将子文件夹名称拆分为 MySQL 插入
【发布时间】:2011-10-05 11:29:31
【问题描述】:

我有一个名为“Comics”的文件夹和该目录中的子文件夹,其名称为漫画 + 问题

例子:

/Comics <-- Main Folder
    /Amazing Spider-Man 129 <-- Sub Folder
    /Hellblazer 100 <-- Sub Folder
    /Zatanna 01 <-- Sub Folder

现在我要做的是扫描 Comics 目录并将每个文件夹名称输出为 mysql 插入查询。实际文件夹名称需要分隔为“漫画名”和“漫画期”。

示例查询

mysql_query("INSERT INTO comics (name, issue) VALUES ('Amazing Spider-Man', '129')");

到目前为止,我想添加一个查询检查以查看漫画是否存在。

<?php
    $main_folder = 'K:/Comics/'; // should be K:\Comics\ but I changed it because of the highlighting issue
    $folders = glob($main_folder.'* [0-9]*', GLOB_ONLYDIR);

    $comics_series = array();
    foreach($folders as $folder){
        $comics_series[] = preg_split('/(.+)\s(\d+)/', str_replace($main_folder, '', $folder), -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
    }

    $values = array();
    foreach($comics_series as $pair){
        $values[] = "('".mysql_real_escape_string($pair[0])."', '".((int) $pair[1])."')";
    }

    $check_query = mysql_query("SELECT * FROM comics WHERE name='".$values[0]."' AND issue='".$values[1]."'");
    if ($check_query == '0'){
        $query = 'INSERT INTO comics (name, issue) VALUES '.implode(',', $values);
        $result = mysql_query($query);
            echo ($result) ? 'Inserted successfully' : 'Failed to insert the values';
    }
    ?> 

query_check 的格式是否正确?

【问题讨论】:

  • 向我们展示您到目前为止所做的尝试以及您遇到的问题。
  • 所以你在子文件夹中有漫画的问题吗?问题文件夹本身,还是您要存储在数据库中的文档?
  • /comics 包含以漫画名称+问题命名的子文件夹,这些子文件夹内是图像,我只想扫描主目录并获取文件夹名称并将它们插入到 mysql 中。
  • 我必须同意@Catfish - 你试过什么?你被困在哪里了?否则这个问题听起来很像“免费为我编写代码”
  • 我添加了我所拥有的...抱歉并不是说我需要它为我完全编码:D 只是坚持将文件名拆分为名称 + 问题。

标签: php mysql directory


【解决方案1】:

如果您只是在拆分漫画名称加问题时遇到困难,请查看explode 函数

如果您在文件/文件夹遍历方面遇到困难,我建议您查看glob。这要容易得多。

<?php
if ($handle = opendir('K:\Comics')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {

            $comicName = null;
            $pieces = explode(' ', $file); // explode the $file

            // if the last element is a number..
            if(is_numeric(end($pieces))) {
                $comicIssue = end($pieces);  // set the issue to a variable
                array_pop($pieces);          // remove the last element of the array

                // loop through the rest of the array and put the pieces back together 
                foreach($pieces as $value) {
                    $comicName .= " $value"; // append the next array element to $comicName
                }
            } else {
                echo "not issue number";
            }
        }
    }
    closedir($handle);
}

?>

【讨论】:

  • 我真的没有看到使用爆炸的方法,不能使用空格,因为某些漫画名称中有空格,有没有办法检查直到出现数字?如果那里根本没有数字怎么办?
  • 您可以使用爆炸,然后抓取爆炸数组中的最后一个元素。那将是问题编号。
  • 如果没有问题#,只是漫画名,还能用吗?
  • 您可以使用 is_numeric 函数进行检查 - php.net/manual/en/function.is-numeric.php
  • 我已经编辑了我的答案。看看这是否会引导你朝着正确的方向前进。
【解决方案2】:
<?php
$main_folder = './Comics'; // should be K:\Comics\ but I changed it because of the highlighting issue
$folders = glob($main_folder.'* [0-9]*', GLOB_ONLYDIR);

$comics_series = array();
foreach($folders as $folder){
    $comics_series[] = preg_split('/(.+)\s(\d+)/', str_replace($main_folder, '', $folder), -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
}

$values = array();
foreach($comics_series as $pair){
    $values[] = "('".mysql_real_escape_string($pair[0])."', '".((int) $pair[1])."')";
}

$query = 'INSERT INTO comics (name, issue) VALUES '.implode(',', $values);
$result = mysql_query($query);
echo ($result) ? 'Inserted successfully' : 'Failed to insert the values';
?>

【讨论】:

  • @Matthew Warner:您是否在 php 脚本的顶部设置了与数据库的连接?喜欢&lt;?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');...
  • @Matthew Warner:尝试在最后一个 echo 之前添加 echo $query;,然后告诉我输出是什么。
  • 我得到了它的工作,需要更改目录,我尝试了 K:\Comics\,这就是给我错误的原因,将其更改为 K:/Comics/,它可以完美运行!!!谢谢。
  • 这是检查漫画是否存在的正确说法吗? $check_query = mysql_query("SELECT * FROM comics WHERE name='".$values[0]."' AND issue='".$values[1]."'");
  • @Matthew Warner:为什么不对(name,issue) 施加UNIQUE 约束,然后对INSERT ... ON DUPLICATE 施加约束?我认为如果你为此写一个不同的问题会更好,因为还需要其他细节(例如表模式、索引、唯一约束)等......