Cakephp验证文件上传“多个”

时间:2014-06-12 10:02:31

标签: php validation cakephp

我正在尝试验证cakephp文件上传

以下是我视图中的输入

<?php echo $this->Form->input('images.', array('type' => 'file', 'multiple', 'label'=>'Upload Images to your gallery')); ?>

这是我在浏览器中获得的HTML代码

<input type="file" required="required" id="ProjectImages" multiple="multiple" name="data[Project][images][]" />

以下是我的验证模型中的代码

'images[]' => array(
        'extension' => array(
            'rule' => array(
                'extension' => array('jpeg', 'png', 'jpg'),
                'message' => 'Please supply valid images'
            )
        ),
        'size' => array(
            'rule' => array('fileSize', '<=', '2MB'),
            'message' => 'Image must be less than 2MB'
        )
    )

我还尝试使用&#39; image&#39;进行验证。作为字段名称,但两者都不起作用。文件正在正确上传,但验证无效。

请帮忙。谢谢

3 个答案:

答案 0 :(得分:1)

请检查此处链接的主题 - Cakephp: Multiple files upload field sets to required automatically

我建议您尝试将模型中的验证命名为images而不是images[]。如果仍然无效,我建议您编写自定义{ {3}}在您的模型中运行并自行处理验证。您可以使用以下功能进行验证。

public function isValidImageFile($filename) {
    if($this->checkFileUploadedName($filename) && 
        $this->checkFileUploadedLength($filename) && 
            $this->checkImgFileExtn($filename)) {
        return true;
    }
    return false;
}

private function checkFileUploadedName($filename)
{
    return (bool) ((preg_match("`^[-0-9A-Z_\.]+$`i",$filename)) ? true : false);
}

private function checkFileUploadedLength($filename)
{
    return (bool) ((mb_strlen($filename,"UTF-8") < 225) ? true : false);
}

private function checkImgFileExtn($filename) {
    $file_parts = pathinfo($filename);
    $supportedFileTypes =  array('jpg', 'png', 'jpeg', 'bmp');
    if(in_array(strtolower($file_parts['extension']), $supportedFileTypes)) {
        return true;
    }
    return false;
}

您可以使用另一种方法来检查文件大小。您可以使用php - beforeSave方法检查filesize。

答案 1 :(得分:1)

尝试使用名称images代替images[],并更正扩展规则,请参阅Document Here

'images' => array(
        'extension' => array(
            'rule' => array(
                'extension',array('jpeg', 'png', 'jpg')
            ),
                'message' => 'Please supply valid images'
          ),
        'size' => array(
            'rule' => array('fileSize', '<=', '2MB'),
            'message' => 'Image must be less than 2MB'
        )
    )

答案 2 :(得分:0)

mimeType或fileSize等验证规则似乎不适用于多个上传阵列,您必须通过自定义规则进行验证:

$validator->add('uploads', [
    'fileSizeSum' => [
        'rule' => function($value, $context) {
            $fileSizeSumLimit = Configure::read('uploads')['max_uploadsize'];
            $fileSizeSum = 0;
            foreach($value as $key => $val) {
                $fileSizeSum += $val['size'];
                if( $fileSizeSum > $fileSizeSumLimit || $val['size'] > $fileSizeSumLimit ) {
                    return false;
                }
            }
            return true;
        },
        'on' => function ($context) {
            return ! empty($context['data']['uploads'][0]['size']);
        },
        'message' => __('Uploadlimit x MB!'),
    ],
    'extension' => [
        'rule' => function($value, $context) {
            $supportedFileTypes =  Configure::read('uploads')['allowed_mimetypes'];
            foreach($value as $key => $val) {
                $ext = pathinfo($val['name'], PATHINFO_EXTENSION);
                if( ! empty($ext) && ! in_array($ext, $supportedFileTypes) ) {
                    return false;
                }
            }
            return true;
        },
        'on' => function ($context) {
            return ! empty($context['data']['uploads'][0]['type']);
        },
        'message' => __('Allowed Mimetypes: '). implode(', ', Configure::read('uploads')['allowed_mimetypes_userrequest']),
    ]
]);