我正在尝试使用其他文本字段以正常格式上传文件.
到目前为止,文件已上传到临时文件夹,但未上传到我的目的地文件夹,我总是收到此错误“文件’上传’被非法上传.这可能是攻击”.
我已经检查了tempfile的文件名,并且在正确的文件夹中具有正确的url.
我在这里想念什么.
$form = new Zend_Form();
$form->setAttrib('enctype', 'multipart/form-data');
$form->setMethod('post')
->addElement('file', 'pdf', array(
'size' => '40',
'label' => 'Select File',
'required' => true,
'validators' => array(
'Size' => array('min' => 20, 'max' => 1000000)
)
)
)
->addElement('submit', 'Save')
;
if ( $this->getRequest()->isPost() ) {
if ( $form->isValid($this->getRequest()->getParams()) ) {
$id = $form->getValue('name');
$upload = new Zend_File_Transfer_Adapter_Http();
$uploadDestination = APPLICATION_PATH . '/../public/uploads/'.$id;
if(!is_dir($uploadDestination)){
mkdir($uploadDestination, 0777, true);
}
$upload->setDestination($uploadDestination);
echo $upload->getFileName();
if($upload->receive('pdf'))
{
echo '<pre>';
print_r($form->getValues());
die();
}
else
{
$messages = $upload->getMessages();
echo implode("\n", $messages);
die();
}
$upload-> receive(‘pdf’);是什么无法正常工作.
解决方法:
认为自提出此问题以来,Zend Framework的情况可能有所改善.
下面的代码显示了可靠的文件验证的有效示例,其中包括自定义的错误消息.
关键是Zend_Form :: isValid()方法就是您所需要的,您无需单独验证文件传输
您的表单定义,请注意,文件验证器的添加就像它们是普通验证器一样
class Jogs_Form_ImportForm extends Zend_Form
{
public function init()
{
$this->setAttrib('enctype', 'multipart/form-data');
$this->setAttrib( 'id', 'form-import' );
$importAction = $this->addElement('radio', 'importAction', array(
'multiOptions' => array(
'components' => 'Import components',
'layouts' => 'Import layouts',
'layoutComponents' => 'Import layout components',
),
'required' => true,
'label' => 'Import Type:',
));
$upload = $this->addElement( 'file', 'import-file', array(
'label' => 'Text (tab delimited) file (.txt)',
'validators' => array(
'Size' => array('max' => 10*1024*1024),
'Extension' => array('txt', 'messages' => array(
Zend_Validate_File_Extension::FALSE_EXTENSION
=> 'file must end with ".txt"' ) ),
'MimeType' => array( 'text/plain', 'messages' => array(
Zend_Validate_File_MimeType::FALSE_TYPE
=> 'file must be text (tab delimited)' ) ),
)
) );
$go = $this->addElement('submit', 'go', array(
'required' => false,
'ignore' => true,
'label' => 'Go',
));
}
}
您的控制器类
class ImportController extends Zend_Controller_Action
{
public function indexAction(){
$form = new Polypipe_Form_ImportForm();
$this->view->form = $form;
if (
$this->getRequest()->isPost()
&&
$form->isValid( $this->getRequest()->getPost() )
){
$data = $form->getValues();
// get the file info
$ft = $form->getElement('import-file')->getTransferAdapter();
$fileInfo = $ft->getFileinfo();
}
}
}