Home > Software engineering >  Upload Multiple Images with php & mysql
Upload Multiple Images with php & mysql

Time:01-16

I need to upload multiple images with php and MySQL but every time I try to upload it's only upload 1 image to database but in the file (uploads\companies) it shown me 4 images

I tried, but I don't find any solution so if anyone can help me

This is my code

$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 >';
                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 >
                    <div >Success, Company Added Successfully</div>
                </div>
            <?php
        }

CodePudding user response:

  1. Initialize the following variable before the start of FOR Loop:
$imageNameStoreForDB='';
  1. Add the following line code after your move_uploaded_file function before your for loop ends to concatenate all images' names:
$imageNameStoreForDB .= $imageNameStore." , ";
  1. Replace the Query as below to use the new variable:
$stmt->execute(array($name, $field,$address,$email,$mobile,$phone,$description,$mapLink, $imageNameStoreForDB));

Note: It will save all images names in the DB separated by "," comma and if you wanna fetch the record then use explode function for images to separate each image.

  • Related