Reputation: 105
How to upload multiple images using PHP? This code working fine for upload single file, but need add function for upload multiple images.
<?php
$result = 0;
define('FORUM_PATH', 'full_patch');
require_once (FORUM_PATH . 'api_member_login.php');
$ipbMemberLoginApi = new apiMemberLogin();
$ipbMemberLoginApi->init();
$member = $ipbMemberLoginApi->getMember();
$id = ($member['member_id']);
$foldername = date("Y_m_d");
$uploads_folder = "uploads";
$upload_folder = "uploads/$id/$foldername";
if (file_exists($upload_folder)) {
} else {
mkdir("$uploads_folder/$id", 0755);
mkdir("$upload_folder", 0755);
}
if (is_uploaded_file($_FILES['myfile']['tmp_name']) && ($_FILES["myfile"]["size"] <= 18192 * 1 * 18192)) {
$enabled = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
list($width, $height, $extension) = getimagesize($_FILES['myfile']['tmp_name']);
if (in_array($extension, $enabled)) {
$newname = uniqid() . "_" . $_FILES['myfile']['name'];
$imglink = $upload_folder . '/' . $newname;
if (@move_uploaded_file($_FILES['myfile']['tmp_name'], "$upload_folder/" . $newname))
include ("class.ExifCleaning.php");
ExifCleaning::adjustImageOrientation($imglink);
$max_width = 1024;
$max_height = 800;
$im = new Imagick();
$im->readImage($imglink);
$im->stripImage();
$im->resizeImage(min($im->getImageWidth(), $max_width), min($im->getImageHeight(), $max_height), imagick::FILTER_CATROM, 1, true);
if (file_put_contents($imglink, $im)) {
require ('watermarkImagick.php');
$result = 1;
}
} else {
$result = 0;
}
}
?>
HTML:
<form id="imgUplForm" action="api.php" method="post" enctype="multipart/form-data" target="upload_target" onchange="startUpload();">
<div id="imageUpload">Upload
<p id="myf1_upload_process"><img src="uploadstat.gif" border="0" /></p>
<p id="myf1_upload_form" align="left">
<input type="file" id="file_browse" name="myfile" onchange="this.form.submit()" accept="image/*" />
</p>
</div>
<iframe id="upload_target" name="upload_target" style="display:none;width:0;height:0;border:0px solid #fff;"></iframe>
</form>
After upload added some functions, ex.: Resize, Exif Cleaning... Need all this functions for uploaded images.
Upvotes: 1
Views: 212
Reputation: 6388
You can upload multiple files in this way
Input field must be defined as an array i.e. images[]
It should be defined as multiple="multiple"
<input name="images[]" type="file" multiple="multiple" />
// Count # of uploaded files in array
$total = count($_FILES['images']['name']);
// Loop through each file
for( $i=0 ; $i < $total ; $i++ ) {
//Get the temp file path
$tmpFilePath = $_FILES['images']['tmp_name'][$i];
//Make sure we have a file path
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['images']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}
For more details PHP Multiple Upload
Upvotes: 1