验证cakephp中的输入文件 [英] validation on a input file in cakephp

查看:147
本文介绍了验证cakephp中的输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cakephp我试图检查一个文件输入字段是否有一个附加的文件,如果不输出错误。

In cakephp I am trying to check if an file input field has a file attached, and if not output an error. I have done this with other fields but just can't seem to get this to work on that field.

这是模型

array('notempty'),
'uploadeduploaded_file'=> array('notempty')
);
?>

array('notempty'), 'uploadeduploaded_file' => array('notempty') ); ?>

这里是我的ctp文件

<?php echo $form->input('Uploaded.uploaded_file', array('type' => 'file', 'label' => 'Upload file', "label" => false)); ?>

我猜想它必须与模型中的字段相关,

I am guessing that it must be something to do with what I should call the field in the model, but I have tried all sorts of combinations and can't get it work.

任何帮助都会感激。

推荐答案

http://stackoverflow.com/questions / 3146933 / cakephp-optional-validation-for-file-upload

这可能有助于

var $validate = array(
            'imageupload' => array(
                'checksizeedit' => array(
                    'rule' => array('checkSize',false),
                    'message' => 'Invalid File size',

                ),
                'checktypeedit' =>array(
                    'rule' => array('checkType',false),
                    'message' => 'Invalid File type',

                ),
                'checkuploadedit' =>array(
                    'rule' => array('checkUpload', false),
                    'message' => 'Invalid file',

                ),
);



function checkUpload($data, $required = false){
        $data = array_shift($data);
        if(!$required && $data['error'] == 4){
            return true;
        }
        //debug($data);
        if($required && $data['error'] !== 0){
            return false;
        }
        if($data['size'] == 0){
            return false;
        }
        return true;

        //if($required and $data)
    }

    function checkType($data, $required = false){
        $data = array_shift($data);
        if(!$required && $data['error'] == 4){
            return true;
        }
        $allowedMime = array('image/gif','image/jpeg','image/pjpeg','image/png');
        if(!in_array($data['type'], $allowedMime)){
            return false;
        }
        return true;
    }

    function checkSize($data, $required = false){
        $data = array_shift($data);
        if(!$required && $data['error'] == 4){
            return true;
        }
        if($data['size'] == 0||$data['size']/1024 > 2050){
            return false;
        }
        return true;
    }

这篇关于验证cakephp中的输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆