且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在php mysql中使用重命名上传多个图像?

更新时间:2023-02-23 22:24:39

<?php
session_start();
include_once('configuration.php');
include_once('db.php');
    if (isset($_REQUEST['submit'])){
        $sqlMax = "SELECT * FROM `shop_list` ORDER BY `shop_id` DESC LIMIT 0, 1"; 
        $sqlMaxResult = $db->fetchQuery($sqlMax);
        $maxId = (int)$sqlMaxResult[0]['shop_id'] + 1;
        $imageList = array();

        foreach($_FILES['shop_img']['name'] as $k=>$v){
            $type = $_FILES['shop_img']['type'][$k];
            $name = $_FILES['shop_img']['name'][$k];
            $temp_name = $_FILES['shop_img']['tmp_name'][$k];
            $imgStr = $maxId.'-'.$name;
            $imgUniqName = uniqid().'.'.$type;
                if(move_uploaded_file($temp_name,"uploaded/" . $name)){
                    array_push($imageList,$imgStr);
                }
            }
          $sql="INSERT INTO `shop_list` (`shop_img`)
            VALUES (:shop_img);";
            $sql_result = $db->queryPrepared($sql,array(
                ':shop_img' =>implode("#",$imageList)
            ));
        $msg = "<b style='color: green;'>Image upload sucessfully</b>";
    }
?> 
<form action="" enctype="multipart/form-data" method="post">
    <?php if(isset($msg)) echo $msg; ?>
    <h3><a>Store Image</a></h3>
    <table>
        <tr>
            <td>Store</td>
            <td><input type="file" class="form-control" name="shop_img[]" id="shop_img" required="true" multiple /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" value="Submit" name="submit" class="btn" /></td>
        </tr>
    </table>
</form>