0

I need to upload some files (images but it doesn't matter) using jquery and php. I find a jquery plugin plugin to make multiple upload file, http://www.fyneworks.com/jquery/multiple-file-upload/ . So i have these two input:

<script type="text/javascript" language="javascript">                  
      $(function(){                    
        $('#immagini').MultiFile({       
        list: '#lista-immagini'          
      });           
      });            
</script>     
<input type="file" name="immagini[]" id="immagini" accept="gif|jpg|tiff" maxlength="10" />  

<script type="text/javascript" language="javascript">                  
      $(function(){                    
        $('#plan').MultiFile({       
        list: '#lista-plan'          
      });           
      });            
</script>     
<input type="file" name="plan[]" id="plan" accept="gif|jpg|tiff" maxlength="10" />  

And php file that is called from the form is:

$files = array();
$fdata = $_FILES['immagini'];


if(isset($_FILES['immagini']))
if(is_array($fdata['name']) && !empty($fdata['name'])){
        for($i=0;$i<count($fdata['name']);++$i){
            $files[]=array(
                'name'    =>$fdata['name'][$i],
                'tmp_name'=>$fdata['tmp_name'][$i],
                'type' => $fdata['type'][$i],
                'size' => $fdata['size'][$i],
                'error' => $fdata['error'][$i],
        );
        }

        foreach($files as $file){
            $tmpName = $file['tmp_name'];
            $fp = fopen($tmpName, 'r');
            $immagine = fread($fp, filesize($tmpName));
            $immagine = addslashes($immagine);
            $DB->query('INSERT INTO immagini(id_annuncio, immagine) values('.$_GET['id'].', "'.$immagine.'")');
            fclose($fp);
        }
}


    $files = array();
$fdata = $_FILES['plan'];

    if(isset($_FILES['plan']))
if(is_array($fdata['name']) && !empty($fdata['name'])){
        for($i=0;$i<count($fdata['name']);++$i){
            $files[]=array(
                'name'    =>$fdata['name'][$i],
                'tmp_name'=>$fdata['tmp_name'][$i],
                'type' => $fdata['type'][$i],
                'size' => $fdata['size'][$i],
                'error' => $fdata['error'][$i],
            );
        }
        foreach($files as $file){
            $tmpName = $file['tmp_name'];         
            $fp = fopen($tmpName, 'r');
            $immagine = fread($fp, filesize($tmpName));
            $immagine = addslashes($immagine);
            $DB->query('INSERT INTO planimetrie(id_annuncio, planimetria) values('.$_GET['id'].', "'.$immagine.'")');
            fclose($fp);
        }
}

The problem is that when i upload files only from one input, the other input upload anyway an empty file. How can I do?

Thanks, Mattia

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
pindol
  • 2,110
  • 6
  • 35
  • 52
  • you have `++$i` instead of `$i++` which may be causing problems... – Gavin May 28 '12 at 09:32
  • 1
    @Gavin That only makes a difference if used as part of a larger statement. If you do `$i = 0; $j = $i++;` then `$j` will be 0, but if you do `$i = 0; $j = ++$i;` then `$j` will be 1. On their own, they will have exactly the same effect. [See here](http://php.net/manual/en/language.operators.increment.php) for more info. – DaveRandom May 28 '12 at 09:35
  • I dont think empty is good for testing string values. – allen213 May 28 '12 at 09:43
  • Not sure about is_array($fdata['name']) either, isnt this a value of an array rather than an array – allen213 May 28 '12 at 09:44
  • check the name of the file, if it is empty in the array do not upload it. – Murtaza Khursheed Hussain May 28 '12 at 09:46

1 Answers1

1

The following code works fine for me, so it may be related to what your doing inside the for statements:

<?
    if(isset($_FILES['immagini']))
    {
        foreach($_FILES['immagini']['name'] as $index => $name)
        {
            if($_FILES['immagini']['size'][$index] > 0)
            {
                echo '<br />' . $name;
            }
        }
    }

    if(isset($_FILES['plan']))
    {
        foreach($_FILES['plan']['name'] as $index => $name)
        {
            if($_FILES['plan']['size'][$index] > 0)
            {
                echo '<br />' . $name;
            }
        }
    }

?>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="immagini[]" />
    <input type="file" name="immagini[]" />
    <input type="file" name="immagini[]" />
    <input type="file" name="plan[]" />
    <input type="file" name="plan[]" />
    <input type="file" name="plan[]" />
    <input type="submit" />
</form>

You just then need to do the inserts where the code currently echo's the name of the file uploaded.

Gavin
  • 6,284
  • 5
  • 30
  • 38