Reputation: 391
I have a website that allows users to upload file, but I would like to set the min size of the file before it's saved into the server. Let's say the min filesize to be uploaded is 50KB. How to write script in Yii? where should I put the function, in controller or model?
Upvotes: 1
Views: 2359
Reputation: 1132
if u using yii
array('filename','file', 'types'=>'jpg, png', 'minSize'=>1024 * 1024 * 10, 'tooLarge'=>'Not more than 10MB')
if u using jquery it much better to get the current size and validate ,
$("#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");
}
});
Upvotes: 3
Reputation: 149
FileValidator has minSize parameter as described here
You can modify your validation rule to smth like this:
array('yourfile','file', 'types'=>'jpg, gif, png, jpeg', 'minSize'=>1024 * 1024 * 50, 'tooLarge'=>'File has to be bigger than 50MB')
Upvotes: 3
Reputation: 4342
yii is really not necessary, there's a global var that's created whenever a file is uploaded to the server..
you must ad a form that accepts files
<form enctype="multipart/form-data"></form>
<input type="file" name="file">
in your action, check the uploaded 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
}
Upvotes: 1