用php & mysql上传多张图片

pdsfdshx  于 2023-01-16  发布在  PHP
关注(0)|答案(1)|浏览(181)

我需要用php和MySQL上传多张图片,但每次我尝试上传它只上传1张图片到数据库,但在文件(上传\公司)它显示我4张图片
我试过了,但是我没有找到任何解决办法所以如果有人能帮我
这是我的密码

$name = $_POST['name'];
        $field = $_POST['field'];
        $address = $_POST['address'];
        $email = $_POST['email'];
        $description = $_POST['description'];
        $phone = $_POST['phone'];
        $mobile = $_POST['mobile'];
        $mapLink = $_POST['maplink'];
        // Image Details
        $images = $_FILES['images'];
        $imageName = $images['name'];
        $imageSize = $images['size'];
        $imageTmpName = $images['tmp_name'];
        $imageType = $images['type'];
        // Image Count
        $imagecount = count($imageName);
        // Check For Errors
        $formErrors = [];
        if(empty($name)) { $formErrors[] = 'Name Can Not Be Empty'; }
        if(empty($address)) { $formErrors[] = 'Address Can Not Be Empty'; }
        if(empty($description)) { $formErrors[] = 'Description Can Not Be Empty'; }
        if(empty($field)) { $formErrors[] = 'Field Can Not Be Empty'; }
        if(empty($email)) { $formErrors[] = 'Email Can Not Be Empty'; }
        if(empty($phone)) { $formErrors[] = 'Phone Can Not Be Empty'; }
        if(empty($mobile)) { $formErrors[] = 'Mobile Can Not Be Empty'; }
        if(empty($mapLink)) { $formErrors[] = 'Map Link Can Not Be Empty'; }
        // Loop Through Images
        for($i = 0;$i < $imagecount;$i++) {
            // Images Allowed Extension
            $allowedExtension = ['jpg','jpeg','png'];
            $imageExtensionExp = explode('.', $imageName[$i]);
            $imageExtension = end($imageExtensionExp);
            // Check Errors
            if(empty($imageName[$i])) {
                $formErrors[] = 'Image Can Not be Empty';
            }
            if(!empty($imageName[$i]) && !in_array($imageExtension, $allowedExtension)) {
                $formErrors[] = 'This Extension Is Not Allowed';
            }
            if($imageSize[$i] > 5242880) { $formErrors[] = 'Size Can\'t be More 5 MB'; }
            // Generate A Random Name
            $imageNameStore = rand(0,10000000) . '_' . $imageName[$i];
            move_uploaded_file($imageTmpName[$i], 'uploads\companies\\' . $imageNameStore);
        }
        // Print All Errors
        if(!empty($formErrors)) {
            echo '<div class="error-container">';
                foreach ($formErrors as $error) {
                    echo '<h4>' . $error . '</h4>';
                }
            echo '</div>';
        }
        // Add To Database
        if(empty($formErrors)) {
            // Add Items To Database
            /* $stmt = $conn->prepare("INSERT INTO
                            companies(Name, Field, Address, Email, Mobile, Phone, Description, Map,Images)
                            VALUES(?,?,?,?,?,?,?,?,?)");
            $stmt->execute(array($name, $field,$address,$email,$mobile,$phone,$description,$mapLink, $imageNameStore));
            */
            // Print Success Message
            ?>
                <div class="container">
                    <div class="alert alert-success mt-5 text-center">Success, Company Added Successfully</div>
                </div>
            <?php
        }
mwngjboj

mwngjboj1#

1.在FOR循环开始之前初始化以下变量:

$imageNameStoreForDB='';

1.在for循环结束之前,在move_uploaded_file函数之后添加以下代码行,以连接所有图像的名称:

$imageNameStoreForDB .= $imageNameStore." , ";

1.按如下所示替换查询以使用新变量:

$stmt->execute(array($name, $field,$address,$email,$mobile,$phone,$description,$mapLink, $imageNameStoreForDB));

注:它将保存数据库中所有图像名称,以“,”逗号分隔,如果您想获取记录,则使用图像分解功能来分隔每个图像。

相关问题