【问题标题】:Bulk image insert from multiple folders wiith images in folder wont work来自多个文件夹的批量图像插入,文件夹中的图像不起作用
【发布时间】:2017-06-19 16:36:08
【问题描述】:

大家好,我的 mysql 批量图像插入脚本无法正常工作。我不知道为什么。 我在根目录中有上传脚本,在这个文件夹中有一个名为 img 的文件夹中的图像,一个按类别命名的文件夹,其中包含图像,因此插入到 mysql 中。但他每次都说我“没有匹配的文件插入到数据库中。 "我不知道为什么路径应该没问题。

这里是脚本家伙

$connect = mysql_connect($server,$dbuser,$dbpass);
mysql_select_db($dbname,$connect);




$dirs = array_filter(glob('img/*'), 'is_dir');
foreach ($dirs as $dir) {
    $path  = "img/" . $dir . "/";
    $files = array_map('mysql_real_escape_string', array_filter(glob("{$path}*.*"), 'is_file'));

    if (empty($files)) {
        echo "There were no matching files to insert into the database.";
    } else {
        $insertValues = array();
        foreach ($files as $file) {
            $data           = getimagesize($file);
            $width          = $data[0];
            $height         = $data[1];
            $insertValues[] = "('Titel', {$dir}, '{$file}', '$width', '$height')";
        }
        $query = "INSERT INTO `gbpics` (`gbpictitel`, `gbpiccat`, `gbpicurl`, `gbpicwidth`, `gbpicheight`) VALUES " . implode(', ', $insertValues);
        if (!mysql_query($query)) {
            echo "There was a problem inserting the data.";
            trigger_error("Query failed: $query<br />Error: " . mysql_error());
        } else {
            echo "The data was inserted successfully.";
        }
    }
}
?>

【问题讨论】:

  • 你试过相对路径吗? $path = "./img/" . $dir . "/";
  • @SloanThrasher 是的,试过了,不行
  • @SloanThrasher 还有其他想法吗?
  • @SloanThrasher 他说他没有要插入的文件。所以我认为他找不到类别文件夹。
  • 你有没有试过echo $path;在你第一次构建它之后,看看你得到了什么价值。

标签: php mysql bulkinsert directory bulk


【解决方案1】:

我觉得

$path  = "img/" . $dir . "/";

正在复制 img/ 部分。 应该是

$path  = $dir . "/";

【讨论】:

  • 总是比“不工作”更容易获得更多,但也注意到来自$insertValues[] = "('Titel', '{$dir}', '{$file}', '$width', '$height')"; 的 dir 位周围缺少引号
猜你喜欢
  • 2015-07-17
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多