Freelancer's Playground! New knowledges every now and then

10May/0712

Create Multiple File Upload Form Using Javascript And Php

Currently I'm working on a project that required an upload form to be dynamic. That is when I want more file to upload I don't need to upload lets say 5 at a time, because I can add more field to it. The idea is simple, I just need to manipulate DOM to add the field and modify PHP manual a little bit to match my need. Look below the full source.


<?php
    if(isset($_POST['submit'])){
        //$files = array_values($_FILES["data"]);
        //print_r($_FILES["data"] );die();
        $count = sizeof($_FILES['data']['name']);
        for($i = 0; $i < $count; $i++){
            if ($_FILES['data']['error'][$i] != UPLOAD_ERR_OK) {
                //show error message or silently go to next file.
                echo 'File upload error ' . $_FILES['data']['error'][$i] . '.<br />';
            }else{
                //do what you want to do here...
                //eg : save file, scan it, etc.
                echo 'file ' . $_FILES['data']['name'][$i] . ' is uploaded!<br />';
            }
        }
    }
?>
<html>
  <head>
    <title>.: Multiple Upload Example :.</title>
    <script language="javascript" type="text/javascript">
        function addNewField(){
            var holder = document.getElementById('uploadholder');
            var row    = document.createElement('tr');holder.appendChild(row);
            var field  = document.createElement('td');row.appendChild(field);
                field.innerHTML = '<input type="file" name="data[]" id="" />';
        }
    </script>
  </head>
  <body>
   <form method="post" enctype="multipart/form-data">
    <table id="uploadholder">
        <!--<tr><td><input type="file" name="data[]" id="" /></td></tr>-->
    </table>
    <input type="button" name="tambah" value="Add Field" onclick="javascript:addNewField()" />
    <input type="submit" name="submit" value="submit" />
   </form>
  </body>
</html>

  • http://www.harouny.com/ harouny

    this is a much easier way of doing the javascript part of the code:

    function add_items(target){
    var obj = document.getElementById(target);
    obj.innerHTML+= ”;
    }

  • http://www.harouny.com harouny

    this is a much easier way of doing the javascript part of the code:

    function add_items(target){
    var obj = document.getElementById(target);
    obj.innerHTML+= ”;
    }

  • http://gonde.freevar.coom/ gondez

    Wah bisa cari kode disini nich

  • http://gonde.freevar.coom gondez

    Wah bisa cari kode disini nich

  • v

    doest work!
    Not in IE7 and not uploading in any browser

  • v

    doest work!
    Not in IE7 and not uploading in any browser

  • http://zhac21.multiply.com/ Zhac

    Thx ya, atas segla infonya!
    Sukses Slalu.
    Salam.

  • http://zhac21.multiply.com Zhac

    Thx ya, atas segla infonya!
    Sukses Slalu.
    Salam.

  • martianman

    pernah coba fancyupload ?
    I’ve tried it, success with FF Browser but always failed with IE Browser… :(

  • martianman

    pernah coba fancyupload ?
    I’ve tried it, success with FF Browser but always failed with IE Browser… :(

  • Mipi

    Hello,

    You can try to use an preexisting script. For example you can see a model here:
    http://www.alfafiles.com/index.php?page=inst_upload

    As I remember the script is swfupload. Just google for it.

    Reg.

  • Mipi

    Hello,

    You can try to use an preexisting script. For example you can see a model here:
    http://www.alfafiles.com/index.php?page=inst_upload

    As I remember the script is swfupload. Just google for it.

    Reg.