我有一个网站,允许用户上传文件,但我想设置文件的最小大小之前,它是保存到服务器。假设要上传的最小文件大小为50KB。如何在Yii中编写脚本?我应该把函数放在哪里,在控制器中还是在模型中?
发布于 2014-07-23 12:07:49
FileValidator具有minSize参数as described here
您可以将验证规则修改为smth,如下所示:
array('yourfile','file', 'types'=>'jpg, gif, png, jpeg', 'minSize'=>1024 * 1024 * 50, 'tooLarge'=>'File has to be bigger than 50MB')发布于 2014-07-23 12:32:46
如果你使用yii
array('filename','file', 'types'=>'jpg, png', 'minSize'=>1024 * 1024 * 10, 'tooLarge'=>'Not more than 10MB')如果你使用jquery来获取当前的大小并进行验证,
$("#imageInput").change(function ()
{
var iSize = ($("#imageInput")[0].files[0].size / 1024);
if(iSize>1024 * 1024 * 10)
{
$("#sizemb").html( iSize + "is greater than 10mb");
}
else if (iSize / 1024 > 1)
{
if (((iSize / 1024) / 1024) > 1)
{
iSize = (Math.round(((iSize / 1024) / 1024) * 100) / 100);
$("#sizemb").html( iSize + "Gb");
}
else
{
iSize = (Math.round((iSize / 1024) * 100) / 100)
$("#sizemb").html( iSize + "Mb");
}
}
else
{
iSize = (Math.round(iSize * 100) / 100)
$("#sizemb").html( iSize + "kb");
}
});发布于 2014-07-23 11:17:59
yii真的不是必需的,只要有一个文件上传到服务器,就会创建一个全局变量。
您必须广告一个接受文件的表单
<form enctype="multipart/form-data"></form>
<input type="file" name="file">在您的操作中,检查上传的文件
$name = $_FILES['file']['name']; //name of the file
$size = $_FILES['file']['size']; //size of the file in bytes
if($size < $minSize)
{
//Your code here
}
else
{
//When file does not meet the minimun.
//Your code here
}https://stackoverflow.com/questions/24901139
复制相似问题