CakePHP的文件上传验证器

发布于 2024-10-15 01:28:01 字数 30 浏览 3 评论 0原文

如何在 CakePHP 中创建文件上传验证器?

How to create validator for file uploads in CakePHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

上课铃就是安魂曲 2024-10-22 01:28:01
var $validate = array(
        'imageupload' => array(
            'checksizeedit' => array(
                'rule' => array('checkSize',false),
                'message' => 'Invalid File size',
                'on' => 'update'
            ),
            'checktypeedit' =>array(
                'rule' => array('checkType',false),
                'message' => 'Invalid File type',
                'on' => 'update'
            ),
            'checkuploadedit' =>array(
                'rule' => array('checkUpload', false),
                'message' => 'Invalid file',
                'on' => 'update'
            ),
            'checksize' => array(
                'rule' => array('checkSize',true),
                'message' => 'Invalid File size',
                'on' => 'create'
            ),
            'checktype' =>array(
                'rule' => array('checkType',true),
                'message' => 'Invalid File type',
                'on' => 'create'
            ),
            'checkupload' =>array(
                'rule' => array('checkUpload', true),
                'message' => 'Invalid file',
                'on' => 'create'
            ),
        )
    );






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,$allowedMime = null){
        $data = array_shift($data);
        if(!$required && $data['error'] == 4){
            return true;
        }
        if(empty($allowedMime)){
            $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;
    }

您还可以参考

var $validate = array(
        'imageupload' => array(
            'checksizeedit' => array(
                'rule' => array('checkSize',false),
                'message' => 'Invalid File size',
                'on' => 'update'
            ),
            'checktypeedit' =>array(
                'rule' => array('checkType',false),
                'message' => 'Invalid File type',
                'on' => 'update'
            ),
            'checkuploadedit' =>array(
                'rule' => array('checkUpload', false),
                'message' => 'Invalid file',
                'on' => 'update'
            ),
            'checksize' => array(
                'rule' => array('checkSize',true),
                'message' => 'Invalid File size',
                'on' => 'create'
            ),
            'checktype' =>array(
                'rule' => array('checkType',true),
                'message' => 'Invalid File type',
                'on' => 'create'
            ),
            'checkupload' =>array(
                'rule' => array('checkUpload', true),
                'message' => 'Invalid file',
                'on' => 'create'
            ),
        )
    );






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,$allowedMime = null){
        $data = array_shift($data);
        if(!$required && $data['error'] == 4){
            return true;
        }
        if(empty($allowedMime)){
            $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;
    }

you can also refer to this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文