I always find questions and answers here really useful to my projects. Am currently working on a PHP multiple upload form and hoping this community can assist me. So far the code, works except that If I upload multiple files I get one less file uploaded. e.g if I choose 3 files, I get 2 files being uploaded, if I choose 8 files 7 are uploaded. Below is my code:
<?php
$id = $_SESSION['PHOTO_ID'];
$hd = $_SESSION['PROJECT_NAME'];
$cat = "projects";
//function to get file extension
function getExtension($str)
{
$i = strrpos($str,".");
if(!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$allowedExts = array("jpeg", "jpg");
for ($i = 0; $i < count($_FILES['img']['name']); $i++) {
//set variables for images
$imgname = $_FILES["img"]["name"][$i];
$img_tmp = $_FILES["img"]["tmp_name"][$i];
$exts = getExtension($_FILES["img"]["name"][$i]);
//replace space character with a dash and add timestamp and random numbers
$nm = str_replace(" ","-",$hd);
//$exts = getExtension($val['name']);
$rand = rand();
$img = $rand."-".$nm."-".time().".".$exts;
$target = "../../img/".$img;
move_uploaded_file($img_tmp=$_FILES["img"]["tmp_name"][$i], $target);
}
//end of for loop
}
?>
And the html form is below:
<form method="post" action="demo.php" id="imageform" role="form" enctype="multipart/form-data">
<input type="file" multiple name="img[]" id="images">
<input type="submit" name="submit" id="image_upload" value="Update">
</form>
And the sessions I created but not really useful at the moment.
$_SESSION['PHOTO_ID'] = $_GET['id'];
$_SESSION['PROJECT_NAME'] = $_GET['project'];