0

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'];
Prince
  • 61
  • 1
  • 7
  • see this: http://stackoverflow.com/q/19295746/6388036 – Dumkaaa May 30 '16 at 14:28
  • Thanks @Dumkaaa. i tried it and i still get the same result. When i choose 3 files i get 2 files uploaded to the server. When i choose 4 files i get 3 files uploaded. Any ideas what could be wrong? – Prince May 30 '16 at 14:44
  • This particular piece of code has a syntax error. Have you checked for errors or is that a copying error? – Rasclatt May 30 '16 at 16:07
  • You have an extra bracket `}` in the end of file. What is this: `//end for each`? Do you have a foreach loop somewhere? Your code runs OK here. – Felippe Duarte May 30 '16 at 16:23
  • Yea, you're right. That was a copying error. The extra bracket } is a copying error. The comment //for each should be disregarded too. – Prince May 30 '16 at 17:43

0 Answers0