多文件上传和处理程序

时间:2014-11-26 11:18:05

标签: javascript php

我有一个具有文件上传功能的网站。现在我想进行多文件上传。

这是主文件:

<form action="update.php?KBID=<?=$KBID ?>&offset=<?=$offset ?>&Action=Update&tabName=<?=$secondtabs_param; ?>" method="post" enctype="multipart/form-data" name="form2" id="form2" onSubmit="handleUploadedZipFile($KBID, $file, $zipfilename)" >
Attachments:<input type="file" id="zipfile" name="zipfile" multiple> 
</form>

onclick功能是:

function handleUploadedZipFile($KBID, $file, $zipfilename){    
if (!empty($_FILES['zipfile'])) {
    foreach ($_FILES['zipfile']['name'] as $key => $temp) {
        $name = $_FILES['zipfile']['name'][$key];
        $type = $_FILES['zipfile']['type'][$key];
        $tmp_name = $_FILES['zipfile']['tmp_name'][$key];
        $error = $_FILES['zipfile']['error'][$key];
        $size = $_FILES['zipfile']['size'][$key];

        $zipfilename = $name;

        if ($size > 0) {
            $dir = PROBLEMZIPS."/".sprintf("%05d",$KBID)."/";
            $result = getUploadedFile($tmp_name,$dir,$zipfilename);
        } else {
            $result = false;
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

这不是完整的工作代码,但清楚地了解如何实现您的要求。  您需要稍微修改 handleUploadedZipFile 功能并添加上传的功能:

if (!empty($_FILES['zipfile'])) {
    foreach ($_FILES['zipfile']['name'] as $key => $temp) {
        $name = $_FILES['zipfile']['name'][$key];
        $type = $_FILES['zipfile']['type'][$key];
        $tmp_name = $_FILES['zipfile']['tmp_name'][$key];
        $error = $_FILES['zipfile']['error'][$key];
        $size = $_FILES['zipfile']['size'][$key];

        $zipfilename = $name;

        if ($size > 0) {
            $dir = PROBLEMZIPS."/".sprintf("%05d",$KBID)."/";
            $result = getUploadedFile($tmp_name,$dir,$zipfilename);
        } else {
            $result = false;
        }
    }
}

并使用 zipfile [] 替换zipfile,将文件字段设为数组,即:

<input type="file" id="zipfile" name="zipfile[]" multiple>