2

如何在 Zend Framework 2 中上传多个文件。我已经完成了这项工作。但是,我收到第二个文件的以下错误。

'first-filename' was illegally uploaded. This could be a possible attack. 

供你参考,

            $size   = new Size(array('max' => 1048576));
            $adapter= new \Zend\File\Transfer\Adapter\Http(); 
            $File    = $this->params()->fromFiles('logo');
            $adapter->setValidators(array($size), $File['name']);
            $FileName   = '';

            if (!$adapter->isValid()){
                $dataError = $adapter->getMessages();
                $error = array();
                foreach($dataError as $key=>$row)
                {
                    $error[] = $row;
                }
            } else {
                $adapter->setDestination(path);
                if ($adapter->receive($File['name'])) {
                    // manipulation
                }
            }

            $logoFile   = $this->params()->fromFiles('logo2');
            $adapterLogo    = new \Zend\File\Transfer\Adapter\Http(); 
            $adapterLogo->setValidators(array($size), $logoFile['name']);
            $logoFileName= '';

            if (!$adapterLogo->isValid()){
                $dataError = $adapterLogo->getMessages();
                $error = array();
                foreach($dataError as $key=>$row)
                {
                    $error[] = $row;
                }
            } else {
                $adapterLogo->setDestination(path2);
                if ($adapterLogo->receive($logoFile['name'])) {
                    // manipulation
                }
            }

我该如何解决这个问题?为什么会出现这个错误?

4

1 回答 1

2

你确定你的表格上有正确的编码类型吗?

$form->setAttribute('enctype','multipart/form-data');

也将文件名添加到 isValid 检查中

if($adapterLogo->isValid('logo2')) { /** .. **/ }

如果您使用 Forms 为您验证它比使用这样的验证器要简单得多,请参见以下示例:

http://samsonasik.wordpress.com/2012/08/31/zend-framework-2-creating-upload-form-file-validation/

于 2013-02-18T15:38:21.923 回答