我正在尝试使用zend表单上传文件。我已经尝试了很多方式,但我得到的表单发布数据与文件名但文件没有收到。这是我的代码
**My Form code**
public function testUploadForm(){
$imageviewScript = new Zend_Form_Decorator_ViewScript();
$imageviewScript->setViewScript('image_element.phtml');
$image_name = new Zend_Form_Element_File('image_name');
$image_name->setAttrib('enctype', 'multipart/form-data');
$image_name->addValidator('Extension',false,'jpg,png,gif');
$image_name->addValidator('Count', false, 1)
//$image_name->setRequired(true)
->addDecorator($imageviewScript)
->setDecorators($this->setFieldElementDecorators());
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit')
->setAttrib('class', 'addbutton_6')
->removeDecorator('label')
->setDecorators($this->getRightSubmitDecorators());
$this->addElements(array($image_name,$submit));
}
在控制器中,我用来接收
等数据public function testuploadAction(){
$params= array();
$this->_form->testUploadForm();
if($this->getRequest()->isPost()){
$params= $this->getRequest()->getParams();
$upload= new Zend_File_Transfer_Adapter_Http();
$upload->setDestination(APPLICATION_PATH.'/../images/uploads/');
if($uploads->receive()) {
//Code to process the file goes here
$filesize = $upload->getFileSize('image_name');
的print_r(FILE_SIZE $);出口;
} else {
$errors = $upload->getErrors();
}
//$formData = $_POST->getValues(); //be sure to call this after receive()
print_r($formData);exit;
//$file = $upload->getFileInfo();
// $filename = $upload->getFileName('image_name'); //optional info about uploaded file
}
$this->_form->populate($params);
$this->view->form= $this->_form;
}
当我尝试打印数据时,它会显示如下警告。
Notice: Undefined index: validators in C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php on line 493
Warning: array_search() expects parameter 2 to be array, null given in C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php on line 493
Notice: Undefined index: tmp_name in C:\xampp\htdocs\XXXX\library\Zend\File\Transfer\Adapter\Abstract.php on line 589
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\XXXX\library\Zend\Validate\File\Count.php on line 228
没有语法错误。表格提交正确但文件未在此处收到。 我已经将文件元素更改为Zend文件元素,我在提交表单后遇到了致命错误。
致命错误:在C:\ xampp \ htdocs \ XXXX \ library \ Zend \ File \ Transfer \ Adapter \ Abstract.php中找不到文件传输适配器'消息'“image_name”的未捕获异常'Zend_File_Transfer_Exception':1254 Stack跟踪:#0 C:\ xampp \ htdocs \ XXXX \ library \ Zend \ File \ Transfer \ Adapter \ Abstract.php(572):Zend_File_Transfer_Adapter_Abstract-> _getFiles('image_name')#1 C:\ xampp \ htdocs \ XXXX \ library \ Zend \ Form \ Element \ File.php(435):Zend_File_Transfer_Adapter_Abstract-> isValid('image_name')#2 C:\ xampp \ htdocs \ XXXX \ library \ Zend \ Form.php(1987):Zend_Form_Element_File- > isValid('shreyansh-ipad ...',数组)#3 C:\ xampp \ htdocs \ XXXX \ application \ controllers \ CustomerController.php(41):Zend_Form-> isValid(Array)#4 C:\ xampp \ htdocs \ XXXX \ library \ Zend \ Controller \ Action.php(503):CustomerController-> registerAction()#5 C:\ xampp \ htdocs \ XXXX \ library \ Zend \ Controller \ Dispatcher \ Standard.php(285) ):Zend_Controller_Action-> dispatch('registerAction')#6 C:\ xampp \ htdocs \ XXXXX \ library \ Zend \ Controller \ Fron t.php(934):第1254行的C:\ xampp \ htdocs \ XXXXX \ library \ Zend \ File \ Transfer \ Adapter \ Abstract.php中的Zend_Co
有人可以提示为什么文件没有为我上传。谢谢。
答案 0 :(得分:0)
最后我找到了解决方案。它只是由于表单加密而发生。为了实现某种网站设计,我正在回显视图文件中的每个字段,即.phtml文件。所以,我正在使用正常形式。即使我将加密类型分配给zend表单,它也没有以phtml形式出现,因此文件现在没有上传我设置为
<form enctype="multipart/form-data" method="post" action="" >
现在工作正常。感谢