Zend表单文件上传不验证或上传文件


Zend Form File upload won't validate or upload files

我试图做一个文件上传Zend表单,但我只是不能得到文件上传工作。

这是我的表单代码:

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 server CE,如果这有任何不同的话。

我提前感谢任何可以提供帮助的人!

<标题>编辑:

尝试了MIss poo下面的代码,得到了这个:

Array
(
)
Array
(
    [name] => Array
        (
        )
    [description] => Array
        (
        )
    [file] => Array
        (
        )
    [submit] => Array
        (
        )
)
Array
(
)

绝对没有错误返回…

我有同样的问题,当我从我的表单中删除setMaxFileSize(104857600)时,我得到了错误[fileUploadErrorIniSize] => File 'image' exceeds the defined ini size,所以我不得不改变我的php.ini配置并将其添加到我的表单enctype="multipart/form-data"

使用以下代码获取验证失败的地方。

if ($form->isValid($formData)) {
  //you code
} else {
   print_r($form->getMessages()); //error messages
   print_r($form->getErrors()); //error codes
   print_r($form->getErrorMessages()); //any custom error messages
}

这将跟踪您的验证将失败的地方,然后您可以轻松地解决它。这至少可以让你更好地了解"为什么"。

尝试Zend_File_Transfr_Adapte_Http,这里有一个例子:

    if ($this->_request->isPost()) {
        $formData = $this->_request->getPost();
        if ($form->isValid($formData)) {
            echo "<h1>Valid</h1>";
            /* Uploading Document File on Server */
            $upload = new Zend_File_Transfer_Adapter_Http();
            $upload->setDestination(APPLICATION_PATH . "/../data/models/temp/");
            try {
                 // upload received file(s)
             $upload->receive();
            } catch (Zend_File_Transfer_Exception $e) {
             $e->getMessage();
            }
       } else {
            echo "<h1>Not Valid</h1>";
        }
        echo "<pre>";
        print_r($formData);
        echo "</pre>";
    }
    $this->view->form = $form;
}  

.