ZF2处理文件上传.如何访问上传的文件?

Chr*_*ris 3 php file-upload zend-framework2

我在手册中看到了很多关于创建表单来上传文件的问题和信息.我已经能够设置一个表单来处理文件上传.是否有任何基于Zend的代码用于在文件下载后使用它们.

每个教程或手动参考似乎都有类似于:

if ($form->isValid()) {
    //
    // ...Save the form...
    //
}
Run Code Online (Sandbox Code Playgroud)

我可以使用本机php函数管理上传move_uploaded_file.我是否遗漏了某些东西,或者Zend刚刚回归到使用临时文件名作为其他代码段中的数据片段?

ara*_*ara 6

它在手册中

http://framework.zend.com/manual/2.1/en/modules/zend.form.file-upload.html

// File: MyController.php

public function uploadFormAction()
{
    $form = new UploadForm('upload-form');

    if ($this->getRequest()->isPost()) {
        // Make certain to merge the files info!
        $post = array_merge_recursive(
            $this->getRequest()->getPost()->toArray(),
            $this->getRequest()->getFiles()->toArray()
        );

        $form->setData($post);
        if ($form->isValid()) {
            $data = $form->getData();
            // Form is valid, save the form!
            return $this->redirect()->toRoute('upload-form/success');
        }
    }

    return array('form' => $form);
}
Run Code Online (Sandbox Code Playgroud)

成功上传文件后,$ form-> getData()将返回:

array(1) {
    ["image-file"] => array(5) {
        ["name"]     => string(11) "myimage.png"
        ["type"]     => string(9)  "image/png"
        ["tmp_name"] => string(22) "/private/tmp/phpgRXd58"
        ["error"]    => int(0)
        ["size"]     => int(14908679)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用您获得的数组$form->getData()来处理上传的文件.

您还可以使用名为的过滤器设置目标并重命名.

下面的链接有很好的解释:

http://framework.zend.com/manual/2.1/en/modules/zend.filter.file.rename-upload.html#zend-filter-file-rename-upload

希望这可以帮助.

  • 我正在寻找RenameUpload的链接.我看到所有的手册页都有`//表单有效,保存表单!`,它读给我的是//这里应该是你要找的代码!`.我编辑了答案以突出显示过滤和链接. (2认同)