无法上传多个文件

时间:2011-08-02 10:42:47

标签: php html zend-framework

我似乎无法在同一表单上上传多个文件,如果没有选择所有文件(如果我全部上传,则可以)。

相关代码:

<form id = "form" name = "form" action="<?php echo URLgenerator::getURL('project', 'group-edit', array('id' => $projectGroup->getId()), 'admin'); ?>" method="post" enctype='multipart/form-data'>
        <?php 
            foreach($langs as $lang):
                $langId = $lang->getId();
                $displayName = $lang->getDisplayName();
        ?>
            <fieldset id="In<?php echo $displayName; ?>">
                <legend id ="<?php echo $langId?>">
                    <?php echo $displayName; ?>: 
                </legend> 
                <label for="name-<?php echo $langId; ?>">Name: </label> 
                <input type="text" id="name-<?php echo $langId; ?>" name="name-<?php echo $langId; ?>" style="width: 500px;" value="<?php echo $projectGroup->getName($lang); ?>" />
                <br />
                <label for="imageOff-<?php echo $langId; ?>">Image Off: </label> 
                <input type="file" id="imageOff-<?php echo $langId; ?>" name="imageOff-<?php echo $langId; ?>" />
                <img src="<?php echo $projectGroup->getImageOffURL($lang); ?>" />
                <br />
                <label for="imageOn-<?php echo $langId; ?>">Image On: </label> 
                <input type="file" id="imageOn-<?php echo $langId; ?>" name="imageOn-<?php echo $langId; ?>" />
                <img src="<?php echo $projectGroup->getImageOnURL($lang); ?>" />
            </fieldset>
        <?php 
            endforeach;
        ?>
        <div align="center">
            <input type="submit" value="Edit" id="button1" /> 
            <input type="reset" id="button2" />
        </div>
    </form>

和动作(var_dump用于调试)

public function groupEditAction()
    {
        $id = $this->getRequest()->getParam('id');
        $projectGroup = new ProjectGroup($id);

        $name = Array();
        $langs = LangFuncs::getAllLangs();
        foreach($langs as $lang) {
            $langId = $lang->getId();

            $name[$langId] = $this->getRequest()->getParam("name-$langId");
        }

        $projectGroup->edit(null, $name);

        $upload = new Zend_File_Transfer_Adapter_Http();
        $upload->setDestination(URLgenerator::getTempFolder());
        $upload->receive();
        $info = $upload->getFileInfo();
        var_dump($info);
        return;
        foreach($langs as $lang) {
            $langId = $lang->getId();
            try { 
                if($info["imageOff-$langId"]['tmp_name'] != '') {
                    $projectGroup->uploadImageOff($lang, $info["imageOff-$langId"]['tmp_name']);
                    unlink($info["imageOff-$langId"]['tmp_name']);
                }
                if($info["imageOn-$langId"]['tmp_name'] != '') {
                    $projectGroup->uploadImageOn($lang, $info["imageOn-$langId"]['tmp_name']);
                    unlink($info["imageOn-$langId"]['tmp_name']);
                }
            } 
            catch (Zend_File_Transfer_Exception $e) { 
                $this->_helper->redirector('image-upload', 'error', 'admin', array());
            }
        } 

        $this->_helper->redirector('index', 'project', 'admin', array());
    }

1 个答案:

答案 0 :(得分:1)

所有文件字段的名称相同。在它之后添加[],即

name="imageOff-<?php echo $langId; ?>[]"

由于所有三个都具有相同的名称,所以最后一个(空)覆盖了第一个。