Multiple Images Upload in PHP code

Multiple file upload Using PHP, How to Upload Multiple Images Files With PHP, Multiple Images Upload by PHP Code, Upload Multiple Image and File in PHP, Upload Multiple Images and Store in Database using PHP and MySQLi

 

Today we will talk about how to select multiple file and we can upload the file. If you have learned to select the file single by uplad, then you can easily upload multiple files.

Let’s get started again. First we will create html file and name that file as multipleimages.html Then we will write html code in this file. For this you can see in the file.

multipleimages.html

<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
  Select images: <input type="file" name="files[]" multiple>
  <input type="submit">
</form>
</body>
</html>

Let us talk about the code written in the form of this html.

  1. action = “upload.php” This will call the uploade.php file. In this file we will write php code.
  2. multiple these will select multiple files.
  3. The third attribute is enctype. It define the attribute file’s encoding type. This attribute needs to be define for file uploading.

Now we will define the file on which we will write the script code of multiple upload file in php.

PHP provides you $ _FILES array. By which you can access the file and related attributes. Through this array, you can access file name, file type, file size, temporary name of file, and related errors.

$_FILES[‘files’][‘name’];
This code returns the name of the file to be uploaded.

$_FILES[‘files’][‘type’];
It returns the code file type.

$_FILES[‘files’][‘size’];
It returns the size of the code file.

$_FILES[‘files’][‘tmp_name’];
This code returns the temporary file name.

$_FILES[‘files’][‘error’];
It returns the code errors related to the code file.

To move any file to a server, PHP provides built in move_uploaded_file () function. The syntax of this function is given below.

Now let me explain the file uploading how file is uploaded.

upload.php

<?php
extract($_POST);
    $error=array();
    $extension=array("jpeg","jpg","png","gif");
    foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
            {
                $file_name=$_FILES["files"]["name"][$key];
                $file_tmp=$_FILES["files"]["tmp_name"][$key];
                $ext=pathinfo($file_name,PATHINFO_EXTENSION);
                if(in_array($ext,$extension))
                {
                    if(!file_exists("photo_gallery/".$file_name))
                    {
                        move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$file_name);
                    }
                    else
                    {
                        $filename=basename($file_name,$ext);
                        $newFileName=$filename.time().".".$ext;
                        move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$newFileName);
                    }
                }
                else
                {
                    array_push($error,"$file_name, ");
                }
            }
?>