如何在DB Row中插入多个值从文件夹目录中读取图像并插入它们

时间:2017-03-21 21:07:47

标签: php mysql insert directory

大家好我有一个很好用的PHP脚本,它读取我服务器上目录中的图像文件,并将imagepath插入数据库中。它工作正常,但我需要插入更多的值不仅imagepath也是一个类别和名称,但我不知道如何使查询工作正确的方式它只是图像本身。 我想插入整个路径,而不是仅插入带扩展名的图像和其他2个值,如名称和类别。

这是PHP脚本

<?php





$server = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'dbname';

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





$path = "folder/subfolder/";
$files = array_map('mysql_real_escape_string',array_map('basename',array_filter(glob("{$path}*.*"),'is_file')));
if(empty($files)){
    echo "There were no matching files to insert into the database.";
} else {    
    $query = "INSERT INTO name (picurl) VALUES ('" . implode("'),('",$files) . "')";
    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.";
    }
}?>

我需要的是查询中的内容

$query = "INSERT INTO gbpics (picname, picacat, picurl) VALUES ('" . implode("'),('",$files) . "')";

用整个部分加上图像和扩展名替换带有扩展名的图像。 任何想法?

一些帮助会很棒。 感谢

1 个答案:

答案 0 :(得分:0)

这里解决的是解决方案脚本:

<?php





$server = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'dbname';

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





$path = "folder/subfolder/";
$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)
{
    $insertValues[] = "('value1', 'value2', '{$file}')";
}$query = "INSERT INTO `name` (`picname`, `piccat`, `picurl`) 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.";
    }
}?>