我正在尝试使用 Zend Form 进行文件上传,但我无法让文件上传工作。
这是我的表单代码:
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('modelupload');
$this->setMethod('post');
$this->setAction('/model/upload');
$this->setAttrib('enctype', 'multipart/form-data');
// ... other elements ... //
$file = new Zend_Form_Element_File('file');
$file
->setAttrib('title', 'Select a file to upload.')
->setAttrib('required',"")
->setRequired(true)
->addValidator('Count',false,1)
->addValidator('Size',false,104857600)
->setMaxFileSize(104857600)
->setValueDisabled(true)
->setDestination(APPLICATION_PATH . "/../data/models/temp/")
->setLabel('Select a file to upload.');
$submit = new Zend_Form_Element_Submit('submit', array(
'label' => 'Upload'
));
$submit->removeDecorator('DtDdWrapper');
$this->setDecorators( array( array('ViewScript',
array('viewScript' => 'formViews/_form_upload_model.phtml'))));
$this->addElements(array($name, $description, $file, $submit));
}
这是我的控制器代码:
public function uploadAction()
{
// action body
error_reporting(E_ALL);
$this->view->pageTitle = "Model Upload";
$form = new Application_Model_FormModelUpload();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
echo "<h1>Valid</h1>";
$upload = new Zend_File_Transfer();
$files = $upload->getFileInfo();
$form->getValues();
} else {
echo "<h1>Not Valid</h1>";
}
echo "<pre>";
print_r($formData);
echo "</pre>";
}
$this->view->form = $form;
}
然后当我尝试上传一些东西时,我得到了这个:
Not Valid
Array
(
[name] => title
[description] => description
[MAX_FILE_SIZE] => 104857600
[file] => filename.extension
[submit] => Upload
)
即表单没有通过验证,我不知道为什么。除此之外,该文件没有被上传。我已经尝试了很多东西,现在我无能为力了。如果这有什么不同,我将在我的本地环境中使用 Zend 服务器 CE。
我提前感谢任何人提供的任何帮助!
编辑:
在下面尝试了 MIss poo 的代码并得到了这个:
Array
(
)
Array
(
[name] => Array
(
)
[description] => Array
(
)
[file] => Array
(
)
[submit] => Array
(
)
)
Array
(
)
绝对没有返回错误...