Zend_Form_Element_File and “File exceeds the defined ini size” issue

主宰稳场 提交于 2020-01-04 09:04:15

问题


I'm using Zend_Form_Element_File to validate certain file properties. The file input element in question is created dynamically - ie. it can be in the form, but doesn't need to; however, if it is, and a file has been submitted via it, it needs to meet certain criteria.

I've encountered the "File exceeds the defined ini size" issue, when the form doesn't contain the file element. Is it an intended behavior?

Please bear in mind that the FormUpload should validate for both form elements. I can use different Zend_Forms, depending on the $_FILES array being empty (or not), but it feels like a poor solution, seeing that Zend_Form should validate the data / data fields for me.

Code to replicate the issue:

<?php

class FormUpload extends Zend_Form
{
    public function init()
    {
        $upload = new Zend_Form_Element_File('upload');
        $upload->setRequired(false);
        $this->addElement($upload);
    }
}

var_dump($_POST, $_FILES);
if (!empty($_POST))
{
    $form = new FormUpload();

    if ($form->isValid($_POST))
    {
        $values = $form->getValues();
        var_dump($values);
    }
    else
        var_dump($form->getMessages());
}

?>

<form method='post' enctype="multipart/form-data">
    <input type='hidden' name='something' value='something'/>
    <input type='submit' value='submit'/>
</form>

<form method='post' enctype="multipart/form-data">
    <input type='file' name='upload'/>
    <input type='submit' value='submit'/>
</form>

I'd treat it as a bug - the solution to this problem is to always have an input field with the validated name; otherwise this error will occur.


回答1:


As I've stated in the edit: I'd treat it as a bug - the solution to this problem is to always have an input field with the validated name; otherwise this error will occur (which doesn't have anything to do with the issue).




回答2:


Here is a workaround you can add to override the Zend_Form's isValid() method. There is a legitimate bug out there that generates the same error when no files are uploaded yet it still attempts to validate anyway. Maybe this will help someone out there.

public function isValid($data) {
    $valid = parent::isValid($data);

    $errorCount = 0;
    foreach($this->getElements() as $elem) {

        if ($elem->hasErrors()) {
            $errorCount++;
        }

        // Related issues:
        // http://framework.zend.com/issues/browse/ZF-12159
        // http://framework.zend.com/issues/browse/ZF-10279
        // http://framework.zend.com/issues/browse/ZF-12189 

        if ($elem instanceof Zend_Form_Element_File && !$elem->isRequired() && !isset($_FILES[$elem->getName()]) && $elem->hasErrors()) {
            $elem->clearErrorMessages();
            $elem->setTransferAdapter( 'Http' ); // reset any errors that may be on the transfer adapter
            $errorCount--;
        }
    }

    if ($this->_errorsForced) {
        return false;
    }

    if ($errorCount==0) {
        $this->_errorsExist = false;
    }

    return $errorCount==0;
}



回答3:


You should change upload_max_filesize = 10M in php.ini (not in Zend only). For example, you can reach this file in Wamp with right click on wamp/php/php.ini . It worked for me.




回答4:


Zend_Form_Element_File will add max file size to post_max_size. if upload_max_filesize is set it will set to that.

to change you need to :

$upload->setMaxFileSize($newSize);

But the larger size you can use is upload_max_filesize if set or post_max_size upload_max_filesize is not set.

To change those edit your php.ini or .htaccess.



来源:https://stackoverflow.com/questions/14736870/zend-form-element-file-and-file-exceeds-the-defined-ini-size-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!