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

时间:2013-05-03 07:04:44

标签: php zend-framework zend-form zend-form-element

我正在尝试使用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
(
)

绝对没有错误返回......

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

使用以下代码获取验证失败的位置。

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
}

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

答案 2 :(得分:0)

尝试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;
}