0

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 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
        }
2
  • 1
    Does this answer your question? Multiple file upload in php Commented Jan 15, 2023 at 12:38
  • Hi Marleen Good, but how can i do it in move_uploaded_file $imageNameStore[] = rand(0,10000000) . '_' . $imageName[$i]; move_uploaded_file($imageTmpName[$i], 'uploads\companies\\' . $imageNameStore); Commented Jan 15, 2023 at 13:05

1 Answer 1

1
  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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry Mr Luqman, But Always it shows me first image duplicated! 4010362_FmNkdv6aYAAHKl2.jpeg, 4010362_FmNkdv6aYAAHKl2.jpeg, 262804_FmMFChXWQAQuIE0.jpeg
Yes @MohanedAli, please update your code where concatenation has been made as $imageNameStoreForDB .= $imageNameStore." , ";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.