I am trying to upload multiple images at once within sessions. In my case the user must upload the exact amount of images he stated earlier for one "position" and repeat the action for another "position" with different images againg with the correct amount. Unforunately the upload doesn't work and I don't know why. Should I use $_POST
instead of $_FILES
, or is the foreach
method not correct? Here is the code:
HTML:
<?php
session_start();
include('php/uploadspec.php');
if(!isset($_SESSION['face'][$_SESSION['counter']])){
header("Location: cuberender.php");
}
$speccount = 1;
?>
<div class="counter">
<form method="post">
<div class="form-group">
<p>Please Choose all <?php echo $_SESSION['quantity'] ?> photos for <?php echo $_SESSION['face'][$_SESSION['counter']] ?></p>
</div>
<div class="form-group">
<input type="file" name="images[]" accept="image/*" multiple />
</div>
<div class="form-group">
<input type="submit" name="Submit" value="Submit!" />
</div>
</form>
<?php
if (isset($_POST['Submit'])) {
$imgamount=count($_POST['images']);
if ($imgamount==$_SESSION['quantity']){
uploadspec(session_id(), $speccount);
$_SESSION['counter']++;
header("Location: posteditor.php");
}
}
?>
PHP:
function uploadspec($id, $speccount){
foreach($_FILES["images"]["tmp_name"] as $image) {
$target_file = $id . '/' .$_SESSION['face'][$_SESSION['counter']]. '/specimage'.$speccount;
$speccount++;
move_uploaded_file($image, $target_file);
}
}
Thanks in advance!