CakePhp上传验证

Jam*_*ell 0 php forms cakephp cakephp-2.0

我一直试图让这个工作一段时间,无数的谷歌搜索没有带来任何帮助.

我想知道是否有办法验证表单fileinput中的变量.所以在"上传"表单中我想确保upload [name]不为空.我可以用Cakephp的模型验证来完成这项工作吗?

Sum*_*ngh 5

Cakephp中没有办法验证fileinput字段.

您可以通过自定义验证规则执行此操作,如下例所示

用于查看文件

<?php
    echo $this->Form->file('image');
    echo $this->Form->error('image');
?>
Run Code Online (Sandbox Code Playgroud)

对于模型文件

<?php
    public $validate = array(
        'image' => array(
            'rule' => array('chkImageExtension'),
            'message' => 'Please Upload Valid Image.'
        )
    );



    public function chkImageExtension($data) {
       $return = true; 

       if($data['image']['name'] != ''){
            $fileData   = pathinfo($data['image']['name']);
            $ext        = $fileData['extension'];
            $allowExtension = array('gif', 'jpeg', 'png', 'jpg');

            if(in_array($ext, $allowExtension)) {
                $return = true; 
            } else {
                $return = false;
            }   
        } else {
            $return = false; 
        }   

        return $return;
    }   
?>
Run Code Online (Sandbox Code Playgroud)