mutiple image upload using multiple input - php

I'm trying to upload multiple files from multiple input element for an online application form. I can upload one image using the below script please share how I can upload multiple image using multiple input??
if(!empty($_FILES)){
include 'config.php';
$file = $_FILES['image_file'];
$file_name = $file['name'];
$error = ''; // Empty
$ext = strtolower(substr(strrchr($file_name, "."), 1));
if($validation_type == 1)
{
$file_info = getimagesize($_FILES['image_file']['tmp_name']);
if(empty($file_info)) // No Image?
{
$error .= "The uploaded file doesn't seem to be an image.";
}
else // An Image?
{
$file_mime = $file_info['mime'];
if ($ext == 'jpc' || $ext == 'jpx' || $ext == 'jb2')
{
$extension = $ext;
}
else
{
$extension = ($mime[$file_mime] == 'jpeg') ? 'jpg' : $mime[$file_mime];
}
if(!$extension)
{
$extension = '';
$file_name = str_replace('.', '', $file_name);
}
}
}
else if($validation_type == 2)
{
if (!in_array($ext, $image_extensions_allowed))
{
$exts = implode(', ',$image_extensions_allowed);
$error .= "You must upload a file with one of the following extensions:".$exts;
}
$extension = $ext;
}
if($error == "") // No errors were found?
{
$new_file_name = strtolower($file_name);
$new_file_name = str_replace(' ', '-', $new_file_name);
$new_file_name = substr($new_file_name, 0, -strlen($ext));
$new_file_name .= $extension; // File Name
$move_file = move_uploaded_file($file['tmp_name'],
$upload_image_to_folder.$new_file_name);
if($move_file)
{
$done = 'The image has been uploaded.';
}
}
else
{
#unlink($file['tmp_name']);
}
$file_uploaded = true;
}

You might be looking for multiple browse buttons, try your field name like this:
<input type="file" name="image_file1[]" />
<input type="file" name="image_file1[]" />
<input type="file" name="image_file1[]" />
<input type="file" name="image_file2[]" />
<input type="file" name="image_file2[]" />
<input type="file" name="image_file2[]" />
and then
if(!empty($_FILES['image_file1']['name'])){
$uploaded_file = $_FILES['image_file1'];
for($fi=0; $fi<count($_FILES['image_file1']['name']); $fi++){
$file_name= $uploaded_file['name'][$fi];
$file_type= $uploaded_file['type'][$fi];
$tmp_name= $uploaded_file['tmp_name'][$fi];
$file_size= $uploaded_file['size'][$fi];
}
}
if(!empty($_FILES['image_file2']['name'])){
$uploaded_file = $_FILES['image_file2'];
for($fi=0; $fi<count($_FILES['image_file2']['name']); $fi++){
$file_name= $uploaded_file['name'][$fi];
$file_type= $uploaded_file['type'][$fi];
$tmp_name= $uploaded_file['tmp_name'][$fi];
$file_size= $uploaded_file['size'][$fi];
}
}

Related

Php multiple image upload processing error

In the form and process file below I am trying to upload 3 sizes of multiple images upload and it is uploading 3 size perfectly fine.
But it is uploading the same image on all sizes of the last image selected.
UPDATE
What i have observed that something has to be played with $src i tried below and when i do this images saved black. $src does not accept [$Kv]
foreach($_FILES['file']['tmp_name'] as $src[$Kv]) {
if($extension[$Kv]=="jpg" || $extension[$Kv]=="jpeg" ){
$uploadedfile[$Kv] = $_FILES['file']['tmp_name'][$Kv];
$src[$Kv] = imagecreatefromjpeg($uploadedfile[$Kv]);
}
list($width,$height)=getimagesize($uploadedfile[$Kv]);
////// 1st Size of Image
$newwidth=350;
$newheight=350;
$tmp[$Kv]=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp[$Kv],$src[$Kv],0,0,0,0,$newwidth,$newheight,$width,$height);
}
.
Suppose I selected 3 images to upload.
Image A
Image B
Image C
It is converting and uploading all sizes of all images (3x3) but image shows on all sizes of all images selected is the image of Image C.
Can you please help on this issue that where I am wrong?
Thanks.
form.php
<form action="process.php" method="post" enctype="multipart/form-data">
<div class="col-lg-12 col-md-9 col-sm-12" id="thumb-output">
<div class="m-dropzone dropzone m-dropzone--primary" id="m-dropzone-two">
<h3 class="m-dropzone__msg-title">
Drop files here or click to upload.
</h3>
<input id="files" class="" type="file" name="file[]" multiple>
</div>
</div>
</form>
process.php
$change="";
$abc="";
if(count($_FILES['file']['name']) > 0){
$Kv = 0;
define ("MAX_SIZE","12000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$image =$_FILES["file"]["name"][$Kv];
if ($image) {
foreach($_FILES['file']['name'] as $filename) {
$filename = stripslashes($_FILES['file']['name'][$Kv]);
$extension = getExtension($filename);
$extension = strtolower($extension);
}
foreach($_FILES['file']['size'] as $size) {
$size=filesize($_FILES['file']['tmp_name'][$Kv]);
}
if ($size > MAX_SIZE*1024){
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'][$Kv];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$Kv];
$src = imagecreatefrompng($uploadedfile);
}else {
$src = imagecreatefromgif($uploadedfile);
}
//echo $scr;
list($width,$height)=getimagesize($uploadedfile);
////// 1st Size of Image
$newwidth=350;
$newheight=350;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
////// 2nd Size of Image
$newwidths=400;
$newheights=400;
$tmps=imagecreatetruecolor($newwidths,$newheights);
imagecopyresampled($tmps,$src,0,0,0,0,$newwidths,$newheights,$width,$height);
////// 3rd Size of Image
$newwidthsz=92;
$newheightsz=92;
$tmpsz=imagecreatetruecolor($newwidthsz,$newheightsz);
imagecopyresampled($tmpsz,$src,0,0,0,0,$newwidthsz,$newheightsz,$width,$height);
/////////////////////////////////////////////////////
foreach($_FILES['file']['name'] as $name) {
$name = $_FILES["file"]["name"][$Kv];
$ext = end((explode(".", $name))); # extra () to prevent notice
}
$folderPath = "../images/combo2_images";
if (file_exists($folderPath)){
}else{
mkdir("$folderPath");
}
//mkdir($folderPath);
if ($execute == true) {
if ($type == 'm') {
foreach($_FILES['file']['name'] as $filenames) {
$filenames = "../images/combo2_mimages/m".$idz.'_'.$Kv++.'.'.$ext;
imagejpeg($tmps,$filenames,100);
}
//$savefilenames = 'sc1_'.$id.'.'.$ext;
foreach($_FILES['file']['name'] as $filenamesz) {
$filenamesz = "../images/combo2_mimages/small/m".$idz.'_'.$Kv++.'.'.$ext;
imagejpeg($tmpsz,$filenamesz,100);
}
//$savefilenamesz = 'sc1_'.$id.'.'.$ext;
}
$filename = "../images/combo2_images/".$idz.'.'.$ext;
imagejpeg($tmp,$filename,100);
$savefilename = $idz.'.'.$ext;
if ($_FILES['file']['size'] !== 0 && $_FILES['file']['error'] == 0) {
if ($type == 'm') {
$querys = "insert into items_images
(combo_type, combo_id, item_id, filename, status)
values (2, '$idz', '$mitem', '$savefilename', '$status')
";
$executes = $dba->query($querys);
}
$queryu = "update items_combobox2
set filename = '$savefilename'
where id = '$idz'
";
$executeu = $dba->query($queryu);
imagedestroy($src);
imagedestroy($tmp); ////// 1st Size of Image
imagedestroy($tmps); ////// 2nd Size of Image
imagedestroy($tmpsz); ////// 3rd Size of Image
}
}
}
header("location: all/items/");
echo "<script>parent.document.location.href = 'all/items/';</script>";
exit();
}
}
Finally i got i working as i want.
if(count($_FILES['file']['name']) > 0){
define ("MAX_SIZE","12000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
$i=1; // auto increment number
$uploaddir = "../images/combo2_images"; //a directory inside
foreach ($_FILES['file']['name'] as $name => $value) {
$filename = stripslashes($_FILES['file']['name'][$name]);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
echo "\n This is the extension: ",$extension;
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
//print error message
?>
<h4>Unknown extension!</h4>
<?php
$errors=1;
} else {
$size=filesize($_FILES['file']['tmp_name'][$name]);
if ($size > MAX_SIZE*1024) {
?>
<h4>You have exceeded the size limit!</h4>
<?php
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" && $extension!=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="jpg" || $extension=="jpeg" && $extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension!=="jpg" || $extension!=="jpeg" && $extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefrompng($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=350;
$newheight=350;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$newwidths=400;
$newheights=400;
$tmps=imagecreatetruecolor($newwidths,$newheights);
imagecopyresampled($tmps,$src,0,0,0,0,$newwidths,$newheights,$width,$height);
$newwidthsz=92;
$newheightsz=92;
$tmpsz=imagecreatetruecolor($newwidthsz,$newheightsz);
imagecopyresampled($tmpsz,$src,0,0,0,0,$newwidthsz,$newheightsz,$width,$height);
//$image_name=($i++).$filename.'.'.$extension;
//$newname="files/".$image_name;
//$copied = copy($_FILES['file']['tmp_name'][$name], $newname);
if ($execute == true) {
if ($type == 'm') {
$filenames = "../images/combo2_mimages/m".$idz.'.'.$extension;
//imagejpeg($tmps,$filenames,100);
$copied1 = copy(imagejpeg($tmps,$filenames,100));
$filenamesz = "../images/combo2_mimages/small/m".$idz.'_'.($i++).'.'.$extension;
//imagejpeg($tmpsz,$filenamesz,100);
$copied2 = copy(imagejpeg($tmpsz,$filenamesz,100));
}
$filename = "../images/combo2_images/".$idz.'.'.$extension;
imagejpeg($tmp,$filename,100);
$savefilename=$idz.'.'.$extension;
if ($type == 'm') {
$querys="insert into items_images
(combo_type, combo_id, item_id, filename, status)
values (2, '$idz', '$mitem', '$savefilename', '$status')
";
$executes=$dba->query($querys);
}
$queryu="update items_combobox2
set filename = '$savefilename'
where id = '$idz'
";
$executeu=$dba->query($queryu);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmps);
imagedestroy($tmpsz);
}
if (!$copied) {
?>
<h4>Copy unsuccessfull!</h4>
<?php
$errors=1;
}
}
}
header("location: all/items/");
echo "<script>parent.document.location.href = 'all/items/';</script>";
exit();
}

PHP - multiple file upload, If not upload some field, it will get error

Hello I do the PHP Upload multiple files, but my form does not require to upload all field (require just at least one field upload) so user may not upload the second input field.
The problem is if user upload only 1 file, user will get the error "Invalid file" (final else statement).
But if user upload all 2 fields, it does not have error, I guess the error come from null upload field. So I use if not empty first, but it's not work. How should I do?
<form action="upload.php" method="post" enctype="multipart/form-data">
Image 1 :
<input type="file" name="images[]" />
<br>
image 2 :
<input type="file" name="images[]" /> <br>
<input type="submit" value="Upload" />
</form>
in upload.php page
if(!empty($_FILES))
{
$count = count($_FILES["images"]["name"]);
for($i=1; $i <= $count; $i++)
{
if ((($_FILES["images"]["type"][$i-1] == "image/gif")
|| ($_FILES["images"]["type"][$i-1] == "image/jpeg")
|| ($_FILES["images"]["type"][$i-1] == "image/png"))
&& ($_FILES["images"]["size"][$i-1] < 2000000)) //2 MB
{
if ($_FILES["images"]["error"][$i-1] > 0)
{
echo "File Error : " . $_FILES["images"]["error"][$i] . "<br />";
}
else
{
if (file_exists("path/to/".$_FILES["images"]["name"][$i-1] ))
{
echo "<b>".$_FILES["images"]["name"][$i-1] . " already exists. </b>";
}
else
{
$newname = date('Y-m-d')."_".$i.".jpg";
move_uploaded_file($_FILES["images"]["tmp_name"][$i-1] , "path/to/".$newname);
}
}
}
else
{
echo "Invalid file" ;
exit();
}
}
}
You can change your first line to:
if(isset($_FILES["images"]["name"][0])) {
With one or multiple files this can work
on line with:
echo "Invalid file" ;
exit();
this can be a problem, because if the first file have a problem you exit aplication and not process the second or others.
i suggest to you this code, too:
<?php
if (isset($_FILES["images"]["name"][0])) {
$path_upload = 'path/upload/';
$count = count($_FILES["images"]["name"]);
$allowed_types = array("image/gif", "image/jpeg", "image/png");
$nl = PHP_EOL.'<br>'; // Linux: \n<br> Window: \r\n<br>
for($i=0; $i < $count; $i++)
{
$file_type = $_FILES["images"]['type'][$i];
$file_name = $_FILES["images"]['name'][$i];
$file_size = $_FILES["images"]['size'][$i];
$file_error = $_FILES["images"]['error'][$i];
$file_tmp = $_FILES["images"]['tmp_name'][$i];
if (in_array($file_type, $allowed_types) && $file_size < 2000000) {
if ($file_error > 0) {
echo 'Upload file:'.$file_name.' error: '.$file_error.$nl;
} else {
$path_new_upload = $path_upload.$file_name;
// verify while filename exists
while (file_exists($path_new_upload)) {
$ext = explode('.', $file_name); // explode dots on filename
$ext = end($ext); // get last item of array
$newname = date('Y-m-d_').$i.'.'.$ext;
$path_new_upload = $path_upload.$newname;
}
move_uploaded_file($file_tmp, $path_new_upload);
}
} else {
echo 'Invalid file type to: '.$file_name.$nl;
// continue the code
}
}
}
Can be better, this verify filename to new creation.

PHP uploading multiple images and inserting into them into database

i want to upload multiple images using this code but i can`t , i think it would be by using for each but every time i try the errors come everywhere
i want to insert the whole images paths in one column and after each picture ','
here is the php code
if (isset($_POST['upload_file'])) {
$date = date('Y-m-d h:i:s');
if ($_FILES['image']['name'] != '') {
$temp = explode('.', $_FILES['image']['name']);
$image_name = round(microtime(true)) . '.' . end($temp);
$image_tmp = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
$image_path = '../media/file/'.$image_name;
$image_db_path = '../media/file/'.$image_name;
if ($image_size < '50000000') {
if ($image_ext == 'jpg' || $image_ext == 'png' || $image_ext =='gif' || $image_ext == 'jpeg') {
if (move_uploaded_file($image_tmp, $image_path)) {
$ins_sql = "INSERT INTO files (imgs_paths, date) VALUES ('$image_db_path', '$date')";
if (mysqli_query($conn,$ins_sql)) {
header('Location: index.php');
}else{
$error = '';
}
}else{
$error = '';
}
}else{
$error = '';
}
}else{
$error = '';
}
} else{
$error = '';
}
}
and here the html code
<input type="file" accept="image/*" name="image[]" multiple/>
You're right about using foreach. If you need to upload multiple, you'll need to use it. And it's as simple as:
foreach($_FILES['image'] as $i => $file){
///... other code
$temp = explode('.', $_FILES['image'][$i]['name']);
$image_name = round(microtime(true)) . '.' . end($temp);
$image_tmp = $_FILES['image'][$i]['tmp_name'];
$image_size = $_FILES['image'][$i]['size'];
///... rest of code
}
Since it's an array of images, you need to access each element. Notice how we're using the array index($i) to access the current element in the loop.
As #RiggsFolly stated, you can also use $file in your loop as it is also the current element in the loop. (Instead of $_FILES['image'][$i]...)

PHP video upload and checking video type

I created my upload file with video size and type validation. Only webm, mp4 and ogv file types are allowed and 2gb file size max. My php code:
if (isset($_POST['submit']))
{
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$allowed_extensions = array("webm", "mp4", "ogv");
$file_name_temp = explode(".", $file_name);
$extension = end($file_name_temp);
$file_size_max = 2147483648;
if (!empty($file_name))
{
if (($file_type == "video/webm") || ($file_type == "video/mp4") || ($file_type == "video/ogv") &&
($file_size < $file_size_max) && in_array($extension, $allowed_extensions))
{
if ($_FILES['file']['error'] > 0)
{
echo "Unexpected error occured, please try again later.";
} else {
if (file_exists("secure/".$file_name))
{
echo $file_name." already exists.";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "secure/".$file_name);
echo "Stored in: " . "secure/".$file_name;
}
}
} else {
echo "Invalid video format.";
}
} else {
echo "Please select a video to upload.";
}
}
My html code:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" name="submit" value="Submit">
</form>
I'am always getting "Invalid video format.". I downloaded webm, mp4 and ogv video files from flowplayer website to test my little upload script.
http://stream.flowplayer.org/bauhaus/624x260.webm
http://stream.flowplayer.org/bauhaus/624x260.mp4
http://stream.flowplayer.org/bauhaus/624x260.ogv
Your extensions were not correctly being validated.. try this
if (isset($_POST['submit']))
{
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$allowed_extensions = array("webm", "mp4", "ogv");
$file_size_max = 2147483648;
$pattern = implode ($allowed_extensions, "|");
if (!empty($file_name))
{ //here is what I changed - as you can see above, I used implode for the array
// and I am using it in the preg_match. You pro can do the same with file_type,
// but I will leave that up to you
if (preg_match("/({$pattern})$/i", $file_name) && $file_size < $file_size_max)
{
if (($file_type == "video/webm") || ($file_type == "video/mp4") || ($file_type == "video/ogv"))
{
if ($_FILES['file']['error'] > 0)
{
echo "Unexpected error occured, please try again later.";
} else {
if (file_exists("secure/".$file_name))
{
echo $file_name." already exists.";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "secure/".$file_name);
echo "Stored in: " . "secure/".$file_name;
}
}
} else {
echo "Invalid video format.";
}
}else{
echo "where is my mojo?? grrr";
}
} else {
echo "Please select a video to upload.";
}
}
function fileSelected() {
var inputs = document.getElementsByClassName('myclass');
var input = inputs[0];
var file = input.files[0];
var name = file.name;
var size = file.size;
var type = file.type;
//alert("type: "+type);
if(type!="video/mp4")
{
alert("NOT SUITABLE EXTENSION SELECTED");
}
filesArray.push({ name: name, size: size });
}
<input type="file" accept="video/*" class=" form-control btn btn-primary myclass" required name="file1" id="file1" onchange="fileSelected();">

foreach on multiple fileupload doesn't stop on error

i'm a very big newbie # php. Hope you are considerate :)
i have this script and a litte problem. I want to check in the first foreach part if the select files are images. If the files are images and not to large it should give an ok for upload!
but it's important to check first all images!
the problem i have is for example:
in inputfield 1 i put an image with 1mb
in inputfield 2 i put an textfile
when i press submit the first image from the input is moving into the folder and than stopping on the textfile with an error <- no php ERROR just my own error (no image file or image is too big)
edit: it seems that the if ($upload_ok == true) part doesn't work
first i want that all input fields getting checked (validextension,fileextension,size) and when all inputfields ok the images can move to the folder ( starting with if ($upload_ok == true) ).
if (!empty($_FILES))
{
$upload_ok=array();
foreach($_FILES as $key => $file)
{
$validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
$fileExtension = strtolower(strrchr($file['name'], "."));
if ((in_array($fileExtension, $validExtensions) && ($file['error'] == 0))||$file['error'] == 4)
{
$upload_ok=true;
echo('true');
}
else
{
$upload_ok=false;
$result=false;
echo('false');
}
}
// if(!in_array(false ,$upload_ok))
if ($upload_ok == true)
{
foreach($_FILES as $key => $file)
{
if ($file['error'] == 0)
{
$newNamePrefix = $picName . '_';
$CounterPrefix = sprintf("%02d",(preg_replace("/[^0-9]/","", $key)));
$fileExtension = strtolower(strrchr($file['name'], "."));
$manipulator = new ImageManipulator($file['tmp_name']);
$newImage = $manipulator->resample(1024, 1024);
$manipulator->save($imgRoot . $maschineFolder . $picFolder . $newNamePrefix . $CounterPrefix . $fileExtension);
}
}
}
}
i made a small pastebin with the hole script here
if I understand you correctly...
select this
$upload_ok=array();
foreach($_FILES as $key => $file)
{
$validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
$fileExtension = strtolower(strrchr($file['name'], "."));
if ((in_array($fileExtension, $validExtensions) && ($file['error'] == 0))||$file['error'] == 4)
{
$upload_ok=true;
echo('true');
}
else
{
$upload_ok=false;
$result=false;
echo('false');
}
}
// if(!in_array(false ,$upload_ok))
and replace it with
$error = false;
$validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
foreach(array_keys($_FILES['fileToUpload']['name']) as $key){
if($_FILES['fileToUpload']['error'][$key] == 4) continue;
$fileExtension = strtolower(strrchr($_FILES['fileToUpload']['name'][$key], "."));
$error |= ! in_array($fileExtension, $validExtensions);
$error |= ! $_FILES['fileToUpload']['error'][$key] == 0;
if($error){
$result=false;
break;
}
}
also find this code:
// if(!in_array(false ,$upload_ok))
if ($upload_ok == true)
{
foreach($_FILES as $key => $file)
{
if ($file['error'] == 0)
{
$newNamePrefix = $picName . '_';
$CounterPrefix = sprintf("%02d",(preg_replace("/[^0-9]/","", $key)));
$fileExtension = strtolower(strrchr($file['name'], "."));
$manipulator = new ImageManipulator($file['tmp_name']);
$newImage = $manipulator->resample(1024, 1024);
$manipulator->save($imgRoot . $maschineFolder . $picFolder . $newNamePrefix . $CounterPrefix . $fileExtension);
}
}
}
and replace it with:
if ($error === 0){
foreach(array_keys($_FILES['fileToUpload']['name']) as $key){
if ($_FILES['fileToUpload']['error'][$key] == 0){
$newNamePrefix = $picName . '_';
$CounterPrefix = sprintf("%02d",(preg_replace("/[^0-9]/","", $key)));
$fileExtension = strtolower(strrchr($_FILES['fileToUpload']['name'][$key], "."));
$manipulator = new ImageManipulator($_FILES['fileToUpload']['tmp_name'][$key]);
$newImage = $manipulator->resample(1024, 1024);
$manipulator->save($imgRoot . $maschineFolder . $picFolder . $newNamePrefix . $CounterPrefix . $fileExtension);
}
}
}
and change your inputs from
<input type="file" name="fileToUpload1" id="fileToUpload1" accept="image/*" />
<input type="file" name="fileToUpload2" id="fileToUpload2" accept="image/*" />
<input type="file" name="fileToUpload3" id="fileToUpload2" accept="image/*" />
to this
<input type="file" name="fileToUpload[]" accept="image/*" />
<input type="file" name="fileToUpload[]" accept="image/*" />
<input type="file" name="fileToUpload[]" accept="image/*" />
or this when you need the reference to the image
<input type="file" name="fileToUpload[1]" accept="image/*" />
<input type="file" name="fileToUpload[2]" accept="image/*" />
<input type="file" name="fileToUpload[3]" accept="image/*" />
When checking images, it's best not to rely on the extension. You can do the following to verify the image type:
$info = getimagesize($_FILES['image']['tmp_name']);
$mime = $info['mime'];
$mime should now contain a string such as "image/jpeg". Here's a link to a list of the mime type values.
Well you can use break; and continue; to navigate loops. Not sure what's your desired outcome though.

Categories