PHP Multiple File Uploading

Want more? Explore the library at

Official site


Similar Posts

27 thoughts on “PHP Multiple File Uploading
  1. Nicet tutorial – and I love the idea of using pure PHP, instead of some framework. However, I think the structure with all those nested IFs is a bit of a shame. In any event, thanks for that. It was good.

  2. instead of a confusing foreach() loop, i just made a for() loop like this:


    if (!empty($_FILES['image']['name'][0])) {

    $imageAmount = count($_FILES['image']['name']);

    for ($i=0; $i < $imageAmount; $i++) {

    $imgName = $_FILES['image']['name'][$i];
    $imgTempLoc = $_FILES['image']['tmp_name'][$i];
    $imgSize = $_FILES['image']['size'][$i];
    $imgError = $_FILES['image']['error'][$i];

    $imgExt = explode('.', $imgName);
    $ActualImgExtention = strtolower(end($imgExt));

    $allowed = array('jpg', 'jpeg', 'tiff', 'png');

    if (!in_array($ActualImgExtention, $allowed)) {
    echo 'You cannot upload files of type: ' . end($imgExt);
    if (!$imgError === 0) {
    echo 'There was een error uploading your image!';

    $NewImgName = uniqid('', true);
    $ImgDestination = '../uploads/' . $NewImgName . "." . $ActualImgExtention;
    if(move_uploaded_file($imgTempLoc, $ImgDestination)){
    header("Location: ../index.php?upload=succes");
    header("Location: ../index.php?upload=failed");
    header("Location: ../index.php?cheeter");

  3. I think it's easier to simply make a proper array first.

    $n = count($_FILES['files']['name']);
    $files = [];
    for ($i = 0; $n > $i; $i++) {
    $files[$i]['name'] = $_FILES['files']['name'][$i];
    $files[$i]['type'] = $_FILES['files']['type'][$i];
    $files[$i]['tmp_name'] = $_FILES['files']['tmp_name'][$i];
    $files[$i]['error'] = $_FILES['files']['error'][$i];
    $files[$i]['size'] = $_FILES['files']['size'][$i];

    For two files gives you an array:

    $files = [
    0 => [
    'name' => 'image01.jpg',
    'type' => 'image/jpeg',
    'tmp_name' => 'tmp01.tmp',
    'error' => 0,
    'size' => 2048
    1 => [
    'name' => 'image02.jpg',
    'type' => 'image/jpeg',
    'tmp_name' => 'tmp02.tmp',
    'error' => 0,
    'size' => 1024

    Which is much easier to deal with then. Then you can use the exact same logic as for one file…but in an "foreach" loop.

    Usually Alex and the gang are very good at simplifying the task but this seemed to be unnecessarily complex.

  4. This is a confusing example too much jumping backwards and forward that it almost seems to be intentionally showing off and confusion intended. Sorry but this good coding sucks because it is not straight forward knowledge.

  5. Hi, Thanks for the video. However, I am trying to upload some files using the same logic but I couldnt do so. I placed echo statements at different places to debug my code and figured out that the control is not entering 'foreach' loop. Could you please help?

  6. Brilliant – thanks. Only thing that would have made it totally perfect is to know how to return a success or fail message that doesn't have words like 'array' which nobody really understands

  7. I used this code to upload Images to my website, but for some reason this code doesn't let me upload more than 20 Images. If i try to upload 35 Images, 15 will be dropped.

  8. on some files larger than 2mb not show me any error or message. 
    the weird part is that the files are not uploaded.
    simply do nothing.
    I am very confused

Leave a Reply

Your email address will not be published. Required fields are marked *