0

I wanted to upload multiple pictures at once using PHP but I am new to PHP so I don't understand how to do it. I want to upload a lot of pictures for one model. Like the picture below:

Here is my PHP code:

<?php
include_once('inc/header.php');


if (isset($_REQUEST['add'])) {
    try {
        $name = $_REQUEST['name'];
        $category = $_REQUEST['category'];
        $age = $_REQUEST['txt_age'];
        $height = $_REQUEST['height'];
        $haircolor = $_REQUEST['haircolor'];
        $eyecolor = $_REQUEST['eyecolor'];
        $bust = $_REQUEST['bust'];
        $waist = $_REQUEST['waist'];
        $about = $_REQUEST['about'];
        $image_file = $_FILES["image"]["name"];
        $type  = $_FILES["image"]["type"]; //file name "txt_file"
        $size  = $_FILES["image"]["size"];
        $temp  = $_FILES["image"]["tmp_name"];

        $path="../img/model_images/".$image_file; //set upload folder path

        if (empty($name)) {
            $errorMsg="Please Enter Name";
        } elseif (empty($image_file)) {
            $errorMsg="Please Select Image";
        } elseif ($type=="image/jpg" || $type=='image/jpeg' || $type=='image/png' || $type=='image/gif') { //check file extension
   if (!file_exists($path)) { //check file not exist in your upload folder path
    if ($size < 5000000) { //check file size 5MB
     move_uploaded_file($temp, "../img/model_images/" .$image_file); //move upload file temperory directory to your upload folder
    } else {
        $errorMsg="Your File To large Please Upload 5MB Size"; //error message file size not large than 5MB
    }
   } else {
       $errorMsg="File Already Exists...Check Upload Folder"; //error message file not exists your upload folder path
   }
        } else {
            $errorMsg="Upload JPG , JPEG , PNG & GIF File Formate.....CHECK FILE EXTENSION"; //error message file extension
        }

        if (!isset($errorMsg)) {
            $insert_stmt=$connect->prepare('INSERT INTO tbl_model(model_name,model_category,model_image,model_age,model_height,model_haircolor,model_eyecolor,model_bust,model_waist,model_description) VALUES(:name,:category,:image,:txt_age,:height,:haircolor,:eyecolor,:bust,:waist,:about)'); //sql insert query
            $insert_stmt->bindParam(':name', $name);
            $insert_stmt->bindParam(':category', $category);
            $insert_stmt->bindParam(':image', $image_file);
            $insert_stmt->bindParam(':txt_age', $age);
            $insert_stmt->bindParam(':height', $height);
            $insert_stmt->bindParam(':haircolor', $haircolor);
            $insert_stmt->bindParam(':eyecolor', $eyecolor);
            $insert_stmt->bindParam(':bust', $bust);
            $insert_stmt->bindParam(':waist', $waist);
            $insert_stmt->bindParam(':about', $about);

            if ($insert_stmt->execute()) {
                echo $insertMsg="Model Added Successfully!"; //execute query success message
            }
        } else {
            echo $errorMsg;
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

 ?>

But with this code I can only upload one picture but I want to upload many pictures.

This is my html code

     <div class="form-group col-12">
                            <label for="slideImages" class="col-form-label">Model Image</label>
                            <input type="file" name="image" class="dropify" multiple>
                        </div>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    Does this answer your question? [How to upload multiple image with rename in php mysql?](https://stackoverflow.com/questions/31073379/how-to-upload-multiple-image-with-rename-in-php-mysql) – Vicky Mar 27 '21 at 05:43

1 Answers1

0

In html you miss '[]' after name as you have to send array when you upload images and also make sure you write enctype in form tag as shown below

The form tag must contain the following attributes.
method="post"
enctype="multipart/form-data"
The input tag must contain type="file[]" and multiple attributes.

<form action="/action_page_binary.asp" method="post" enctype="multipart/form-data">
<div class="form-group col-12">
<label for="slideImages" class="col-form-label">Model Image</label>
<input type="file" name="image[]" class="dropify" multiple>
</div>
</form>

after this just you have to write for loop or foreach loop in php file as shown below in your code you use single column to store multiple image so apply this loop after you $about

if (isset($_REQUEST['add'])) {
    try {
        $name = $_REQUEST['name'];
        $category = $_REQUEST['category'];
        $age = $_REQUEST['txt_age'];
        $height = $_REQUEST['height'];
        $haircolor = $_REQUEST['haircolor'];
        $eyecolor = $_REQUEST['eyecolor'];
        $bust = $_REQUEST['bust'];
        $waist = $_REQUEST['waist'];
        $about = $_REQUEST['about'];

        //$path="../img/model_images/".$image_file; //set upload folder path

        $path = '';

        if (empty($name)) {
            $errorMsg="Please Enter Name";
            exit;
        } 
        foreach($_FILES["image"]["name"] as $key => $value){
            $image_file = implode(',', $_FILES["image"]["name"]);
            $image_name = $_FILES["image"]["name"][$key];
            $type  = $_FILES["image"]["type"][$key]; //file name "txt_file"
            $size  = $_FILES["image"]["size"][$key];
            $temp  = $_FILES["image"]["tmp_name"][$key];

            if (empty($image_file)) {
            $errorMsg="Please Select Image";
            exit;
            } else if (
                ($type=="image/jpg" || 
                $type=='image/jpeg' || 
                $type=='image/png' || 
                $type=='image/gif')
            ) { //check file extension
               if (!file_exists($path)) { //check file not exist in your upload folder path
                if ($size < 5000000) { //check file size 5MB
                 move_uploaded_file($temp, "images/" .$image_name); //move upload file temperory directory to your upload folder
                } else {
                    $errorMsg="Your File To large Please Upload 5MB Size"; //error message file size not large than 5MB
                    exit;
                }
               } else {
                   $errorMsg="File Already Exists...Check Upload Folder"; //error message file not exists your upload folder path
                   exit;
               }
                    } else {
                        $errorMsg="Upload JPG , JPEG , PNG & GIF File Formate.....CHECK FILE EXTENSION"; //error message file extension
                        exit;
                    }



        }

        echo $name.$category.$age.$height.$haircolor.$eyecolor.$bust.$waist.$about.$image_file;

        exit;

after this just bind above $image_file in your query

when you want to fetch display image use explode


$explode_images = explode(",", $images_file);

its gives you array then use foreach loop and key to access image name url or whatever you store in that column.

Hope this help .
if you need explanation of implode and explode check this out Implode Explode

Vicky
  • 83
  • 11
  • Where I use this loop? Please explain me I'm new so I don't understand –  Mar 27 '21 at 05:52
  • please check edited answer hope that help – Vicky Mar 27 '21 at 06:22
  • Thank you so much for your help brother, but I stuck with this solution i cannot understand how to complete this code. I know you help me a lot. Thank You for your time. –  Mar 27 '21 at 13:12
  • no worries but where you stuck in code i will try to solve your problem at my best. – Vicky Mar 27 '21 at 15:00
  • Thank you so much brother! Can you complete my given php code. So i can understand better. I can't understand where i used loop or forloop. So if you can complete php code so It will be very grateful for me. –  Mar 27 '21 at 15:12
  • check edited code just connect db change folder where you want to store image if still not understand please comment here. – Vicky Mar 27 '21 at 16:47
  • this code **does not** check the file extension and therefore allows .php files to be uploaded – Your Common Sense Mar 27 '21 at 16:58
  • just copy his code and show him where to use foreach loop not my code – Vicky Mar 27 '21 at 17:04
  • @RichPhp Thank you so much brother, Successfully i uploaded multiple image for a single model. You help me lot. God Bless you. You lost your time for solving my problem. Again Thank You so much –  Mar 27 '21 at 17:54
  • it is my pleasure to help you. ✅ – Vicky Mar 27 '21 at 17:58