到目前为止,这就是我所知道的将数据从帖子传递到表单的方式。
$form->setData( $this->getRequest()->getPost() );
我认为这可能有效
$form
->setData( $this->getRequest()->getPost() )
->setData( $this->getRequest()->getFiles() );
它没有。通过查看框架源代码,我确认它不应该。所以我在考虑将文件数据合并到发布数据中。这肯定不是理想的解决方案吗?这不像 getPost() 和 getFiles() 返回易于合并的数组,它们返回参数对象。
请注意这是 Zend Framework 2 特定的。
最佳答案
您是否尝试过 getFileInfo
现在知道或注意到您正在使用 Zend.通常在每个文件的基础上,$_FILE
是一个基于正在上传的文件的信息的数组。文件名、扩展名等。Zends getFileInfo
以类似的方式输出该信息。虽然我有一段时间没有玩过它,但它值得研究一下
示例概念(我知道更多用于多个文件上传,但与一个很好的概念一起使用,以防万一您想添加第二个或更多文件)
$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
}
}
}
}
关于php - 如何将 $_FILE 和 $_POST 数据传递到表单中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690320/