0

我需要使用 jquery 和 php 上传一些文件(图像但没关系)。我找到了一个 jquery 插件插件来制作多个上传文件,http://www.fyneworks.com/jquery/multiple-file-upload/。所以我有这两个输入:

<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" />  

从表单调用的 php 文件是:

$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);
        }
}

问题是,当我仅从一个输入上传文件时,另一个输入无论如何都会上传一个空文件。我能怎么做?

谢谢, 马蒂亚

4

1 回答 1

1

以下代码对我来说很好,因此它可能与您在 for 语句中所做的事情有关:

<?
    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>

然后,您只需在代码当前回显上传文件的名称的地方进行插入。

于 2012-05-28T09:50:50.540 回答