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.
- action = "upload.php" This will call the uploade.php file. In this file we will write php code.
- multiple these will select multiple files.
- The third attribute is enctype. It define the attribute file's encoding type. This attribute needs to be define for file uploading.
<?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, ");
}
}
?>