php - 无法使用多个文件输入字段。曾德

标签 php html zend-framework

似乎找不到我的问题,但我想做的是为项目组提供一个编辑表单,它可以有 4 张图像。 当我尝试上传所有四张图片时,它有效,当我只上传 1 张时,它没有。

代码:HTML 表单

<?php
    $langs = $this->langs;
    $projectGroup = $this->projectGroup;
?>
<div id="box">
    <h3>Edit Project Group</h3>
    <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>
</div>

<script type="text/javascript">
    <?php 
        foreach($langs as $lang):
            $langId = $lang->getId();
    ?>
    $('#<?php echo $langId; ?>').click(function() {
        $('#div-<?php echo $langId; ?>').toggle();
    });
    <?php 
        endforeach;
    ?>
</script>

代码:编辑 Action

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

当我执行 var_dump 时我得到了什么:

array
  'imageOff-1' => 
    array
      'name' => string '1.gif' (length=5)
      'type' => string 'application/octet-stream' (length=24)
      'tmp_name' => string 'C:\wamp\tmp\php95C1.tmp' (length=23)
      'error' => int 0
      'size' => string '2248' (length=4)
      'options' => 
        array
          'ignoreNoFile' => boolean false
          'useByteString' => boolean true
          'magicFile' => null
          'detectInfos' => boolean true
      'validated' => boolean true
      'received' => boolean false
      'filtered' => boolean false
      'validators' => 
        array
          0 => string 'Zend_Validate_File_Upload' (length=25)
      'destination' => string 'C:/wamp/www/EfCom/public/tmp' (length=28)
  'imageOn-1' => 
    array
      'name' => string '' (length=0)
      'type' => null
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => null
      'options' => 
        array
          'ignoreNoFile' => boolean false
          'useByteString' => boolean true
          'magicFile' => null
          'detectInfos' => boolean true
      'validated' => boolean false
      'received' => boolean false
      'filtered' => boolean false
      'validators' => 
        array
          0 => string 'Zend_Validate_File_Upload' (length=25)
      'destination' => string 'C:/wamp/www/EfCom/public/tmp' (length=28)
  'imageOff-2' => 
    array
      'name' => string '' (length=0)
      'type' => null
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => null
      'options' => 
        array
          'ignoreNoFile' => boolean false
          'useByteString' => boolean true
          'magicFile' => null
          'detectInfos' => boolean true
      'validated' => boolean false
      'received' => boolean false
      'filtered' => boolean false
      'validators' => 
        array
          0 => string 'Zend_Validate_File_Upload' (length=25)
      'destination' => string 'C:/wamp/www/EfCom/public/tmp' (length=28)
  'imageOn-2' => 
    array
      'name' => string '' (length=0)
      'type' => null
      'tmp_name' => string '' (length=0)
      'error' => int 4
      'size' => null
      'options' => 
        array
          'ignoreNoFile' => boolean false
          'useByteString' => boolean true
          'magicFile' => null
          'detectInfos' => boolean true
      'validated' => boolean false
      'received' => boolean false
      'filtered' => boolean false
      'validators' => 
        array
          0 => string 'Zend_Validate_File_Upload' (length=25)
      'destination' => string 'C:/wamp/www/EfCom/public/tmp' (length=28)

附言。 也许我可以通过使用这个临时名称并使用它来让它工作,但在正常情况下,我知道临时名称有一个正常的扩展名而不是 .tmp,所以我使用它作为扩展名。当我获得 .tmp 时,我什么也没做,因为它不是 .jpg、.png 或 .gif(在我的上传功能中)。

如果您也需要上传功能,请评论。

最佳答案

您可能需要将 ignoreNoFile 设置为 true。看看这篇文章是否有帮助:Zend_File_Transfer w/multiple files does not upload equally

关于php - 无法使用多个文件输入字段。曾德,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6910018/

相关文章:

php - MySQL插入重复键

php mkdir 不遵守我正在使用的 chmod 规则

php - 多语言 PHP 网站 URL 文件名更改

php - 持护照的多用户 - 如何获取经过身份验证的用户类型

html - 为什么在这些div元素之间会出现这个不是margin的空格呢?

html - 滚动行为平滑, anchor 标记没有区别

javascript - 从输入元素获取表单和 FormData 对象?

php - Docker 与 Mysql 本地服务器 SQLSTATE[HY000] [2003]

php - 登录 MVC(Zend 框架)

PHP、sendmail 和传输 - 如何加速邮件发送