Zend 2在上传时重命名文件

时间:2013-03-14 11:38:41

标签: zend-framework2

我一直在尝试使用Zend Framework 2上传和重命名文件。我的控制器如下:

    if ($request->isPost()){
        $post = array_merge_recursive(
                $request->getPost()->toArray(),
                $request->getFiles()->toArray()
                );
        $book = new Book();

        $form->setInputFilter($book->getInputFilter());
        $form->setData($post);

        if($post['image'] != ''){
            $originalFileName = pathinfo($post['image']);
            $author = $post['author'];
            $name = $post['name'];
            $new = preg_replace('/[^a-z0-9]+/i', '-', iconv('UTF-8', 'ASCII//TRANSLIT', $author . ' ' . $name));
            $newFileName = $new . '.' . $originalFileName['extension'];

            $inputFilter = $form->getInputFilter();

            $fileInput = new InputFilter\FileInput('image');
            $fileInput->setRequired(false);
            $fileInput->getFilterChain()->attachByName(
                    'filerenameupload',
                    array(
                            'target' => '/public/images/' . $newFileName,
                            )
                    );
            $inputFilter->add($fileInput);
        } 

        if($form->isValid()){           
            $book->exchangeArray($form->getData());
            $this->getBookTable()->save($book);             
            return $this->redirect()->toRoute('book');
        }

但表格没有验证。在我的表单中点击“保存”后,我收到了“找不到文件”。我上传字段旁边的错误。有谁知道,问题出在哪里? Zend官方文档没有帮助:(

0 个答案:

没有答案