我正在尝试使用 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));
}
这是我的 Controller 代码:
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
(
)
绝对没有返回错误...
最佳答案
我也有同样的问题,当我从表单中删除 setMaxFileSize(104857600)
时,我收到错误[fileUploadErrorIniSize] => 文件“image”超出了定义的 ini 大小
,所以我必须更改我的 php.ini 配置并将其添加到我的表单 enctype="multipart/form-data"
中。
关于php - Zend 表单文件上传不会验证或上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16353443/