Core PHP PHP

How to upload multiple images in PHP ?

How to upload multiple images in PHP ?

Upload multiple images with validation of extension in php. And show all images of a folder to find total images.

 

<?php
// Upload multiple files
if($_FILES){
$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($file_name)){
move_uploaded_file($file_tmp=$_FILES[“files”][“tmp_name”][$key],$file_name);
}else{
$filename=basename($file_name,$ext);
$newFileName=$filename.time().”.”.$ext;
move_uploaded_file($file_tmp=$_FILES[“files”][“tmp_name”][$key],$newFileName);
}
}else{
array_push($error,”$file_name, “);
}
}
}
?>

<form action=”” method=”post” enctype=”multipart/form-data”>
<table width=”100%”>
<tr>
<td>Select Photo (one or multiple):</td>
<td><input type=”file” name=”files[]” multiple/></td>
</tr>
<tr>
<td colspan=”2″ align=”center”>Note: Supported image format: .jpeg, .jpg, .png, .gif</td>
</tr>
<tr>
<td colspan=”2″ align=”center”><input type=”submit” value=”Create Gallery” id=”selectedButton”/></td>
</tr>
</table>
</form>

<?php
// Print Images
$files = glob(“*.*”);
echo “View Images”;
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
echo ‘<img src=”‘.$image .'” alt=”Random image” width=”100″ height=”100″ />’;
}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *