douniewei6346 2017-03-21 21:07
浏览 42

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

Hi guys i have a nice working PHP script which reads the image files in a directory on my server and insert the imagepath in database. It is working fine but i need to insert more values not only the imagepath also a category and a name but i cant figure out how to make the query work the right way it only iserts the image itself. I want to insert the whole path instead of only the image with extension and 2 other values like name and category.

Here is the PHP script

<?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.";
    }
}?>

What i need is something like that in the query

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

And replace the image with extension with the whole part plus the image and extension. Any Ideas?

Some help would be great. Thanks

  • 写回答

1条回答 默认 最新

  • doubomudichen0832 2017-03-21 21:21
    关注

    Figured it out here is the solution script:

    <?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.";
        }
    }?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)