doufei2355 2012-11-22 20:49
浏览 8
已采纳

CakePhp上传验证

I've been trying to get this to work for a bunch of hours, countless googling has not resulted in anything helpful.

I was wondering if there was a way to validate on the variables inside of a form fileinput. So like in a form "upload" I want to make sure that upload[name] is not empty. Could I do this at all with Cakephp's model validation?

  • 写回答

2条回答 默认 最新

  • douzhuan1467 2012-11-23 07:00
    关注

    There is no way in Cakephp to validate fileinput field.

    You can do it by custom validation rules like below example

    for view file

    <?php
        echo $this->Form->file('image');
        echo $this->Form->error('image');
    ?>
    

    For model file

    <?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;
        }   
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?