Koushik Mondal
Koushik Mondal

Reputation: 15

How can I upload multiple images with md5 name using php?

<?php
include"db.php";
if(isset($_POST['btn_upload']))
{
 $v1=rand(1111,9999);
 $v2=rand(1111,9999);
 $v3=$v1.$v2;
 $v3=md5($v3);

for($i=0; $i<count($_FILES["file_img"]["name"]); $i++ )
 {
  //select folder id to insert all images
   $folder_name=$_POST['select_folder_name'];


    $filename=$_FILES["file_img"]["name"][$i];
    $filetype=$_FILES["file_img"]["type"][$i];
    $gallery_image_dst="./gallery_image/".$v3.$filename;
    $gallery_image_location="gallery_image/".$v3.$filename;
    move_uploaded_file($_FILES["file_img"]["tmp_name"],$gallery_image_dst);

  //insert image name, path, type and folder name to database.
  $insert_gallery= "INSERT INTO `gallery`(`image_name`, `image_path`, 
  `image_type`,`folder_name`) VALUES 
  ('$filename','$gallery_image_location','$filetype','$folder_name')";

   $result=mysqli_query($conn, $insert_gallery);         
   if($result){
        echo"<script>alert('Successfully uploaded')</script>";
        echo"<script>window.open('index.php?upload_images','_self') 
        </script>";
   }else{
       echo"<script>alert('Oop! Somthing going wrong')</script>";
   }  
  } 
 }
?>

Here I am getting all image location, image type, and image md5 name in my database. But problem is that I'm not getting any images to my gallery_image folder. After Successfully uploaded message I'm getting a error Warning:move_upload_file() expects parameter 1 to be string, array given in c"\xampp........location and line no.

Upvotes: 0

Views: 225

Answers (1)

Nirali
Nirali

Reputation: 1786

you missed index [$i]

You have to update,

 move_uploaded_file($_FILES["file_img"]["tmp_name"][$i],$gallery_image_dst);

Upvotes: 2

Related Questions