验证 cakephp 中的输入文件 [英] validation on a input file in cakephp
本文介绍了验证 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.
这是模型
这是我的 ctp 文件
and here is my ctp file
<?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.
任何帮助将不胜感激
推荐答案
这可能对你有帮助
或
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屋!
查看全文