2

到目前为止,这就是我所知道的将数据从帖子传递到表单的方式。

$form->setData( $this->getRequest()->getPost() );

我认为这可能有效

$form
    ->setData( $this->getRequest()->getPost() )
    ->setData( $this->getRequest()->getFiles() );

它没有。查看框架源代码,我确认它不应该。所以我正在考虑将文件数据合并到帖子数据中。这肯定不是理想的解决方案吗?这不像 getPost() 和 getFiles() 返回容易合并的数组,它们返回 Parameter 对象。

请注意,这是 Zend Framework 2 特定的。

4

4 回答 4

3

您是否尝试过getFileInfo现在知道或注意您使用 Zend 的事实。通常以每个文件为基础$_FILE是一个基于正在上传的文件信息的数组。文件名、扩展名等。ZendsgetFileInfo以类似的方式输出这些信息。虽然好久没玩了,但是值得一看

示例概念(我知道更多关于多个文件上传的信息,但与一个很好的概念一起使用,以防万一你想在路上添加第二个或更多文件)

$uploads = new Zend_File_Transfer_Adapter_Http();
$files  = $uploads->getFileInfo();

foreach($files as $file => $fileInfo) {
    if ($uploads->isUploaded($file)) {
        if ($uploads->isValid($file)) {
            if ($uploads->receive($file)) {
                $info = $uploads->getFileInfo($file);
                $tmp  = $info[$file]['tmp_name'];
                $data = file_get_contents($tmp);
                // here $tmp is the location of the uploaded file on the server
                // var_dump($info); to see all the fields you can use
            }
         }
     }
}
于 2012-07-27T15:58:31.197 回答
3

在尝试使用 Zend 的文件传输适配器后,我在控制器中使用了一种解决方法。我认为表单类中的 setData() 应该将项目合并到数据中,而不是替换它们。(恕我直言)

protected function getPostedData()
{
    if ( is_null($this->postedData) )
    {
        $this->postedData = array_merge(
            (array) $this->getRequest()->getPost(),
            (array) $this->getRequest()->getFiles()
        );
    }
    return $this->postedData;
}
于 2012-08-02T09:05:11.513 回答
1

我正在使用array_merge

    $form    = $this->getForm('my_form');
    $request = $this->getRequest();

    if($request->isPost())
    {

        $file    = $this->params()->fromFiles('name_of_file');
        $form->setData(array_merge(
            $request->getPost()->toArray(),
            array('arquivo' => $file['name'])
        ));

        if ($form->isValid()) {
        // now i can validate the form field
于 2012-11-21T13:48:56.293 回答
0

我使用本文解释的变量变量创建变量,然后将它们作为每个表单条目的值进行回显。

例子:

// create array of GET/POST variables then convert each variable to a local variable
$fields = array_keys($_REQUEST);
foreach ($fields as $field) {
   $$field = $_REQUEST[$field];
}
于 2012-07-27T15:44:26.613 回答