douyun6399 2017-04-26 12:53
浏览 17
已采纳

ZF RenameUpload过滤器无法正常工作

I'm trying to upload an image, very basic. Here's my input filter file:

namespace MyNamespace;

use Zend\InputFilter\InputFilter;
use Zend\Filter\StringTrim;
use Zend\Filter\StripTags;
use Zend\Filter\File\RenameUpload;
use Zend\Validator\File\Size;
use Zend\Validator\File\MimeType;

class MyFilter extends InputFilter 
{
    public function __construct()
    {
        $this->add(
            [
                'name' => 'image',
                'required' => true,
                'filters' => [
                    [
                        /* 'name' => RenameUpload::class, */
                        'name' => 'filerenameupload',
                        'options' => [
                            'target' => './data/uploads/images/.ext',
                            'overwrite' => true,
                            'randomize' => true,
                            'use_upload_extension' => true,
                        ]
                    ]
                ],
                'validators' => [
                    [
                        'name'    => Size::class,
                        'options' => [
                            'max' => '10MB',
                        ],
                    ],
                    [
                        'name' => MimeType::class,
                        'options' => [
                            'mimeType' => [
                                'image/jpg',
                                'image/jpeg',
                                'image/png'
                            ]
                        ],
                    ],
                ],
            ]
        );
    }
...

I checked $request->getFiles()->toArray() and the uploaded image is there. In the controller I do:

$postData = array_merge (
    $this->request->getPost()->toArray(),
    $this->request->getFiles()->toArray()
);

$form = new MyForm;
$form->setInputFilter(new MyFilter);
$form->setData($postData);

$form->isValid()                      // this returns true.

When I check the uploads folder, nothing is there. It's not a filesystem issue since the folder owner is the same as the user running apache; and the permissions are 755.

I also noticed if I delete the folder where it's suppose to write to, $form->isValid() still returns true.

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dream_life5200 2017-04-28 12:40
    关注

    Provide the proper upload directory if you want to use the RenameUpload filter. Like the following

    './data/upload/images/'
    

    Use array_merge_recursive() instead of array_merge(). Because array_merge() orverrides similar array keys (just keeps the last one).

    And this will upload the the image to the target assigned by the RenameUpload filter.

    <?php
    
    ...
    
    if($form->isValid()) {
        // Move uploaded file to the assigned directory.
        $data = $form->getData();
    
        // Do other stuff
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入