Check Post Content-Length

Hello!

I have a server with 2MB of max file size and max post size.

I need to check on my Yii app if the filesize is bigger than 2MB and I have no idea of how to do that.

I can’t change server values, I have to check the file size and if it is bigger add a custom error to model.

Could you help me please?

Thanks!!!

Right now this is the warning: POST Content-Length of 3044251 bytes exceeds the limit of 1048576 bytes in Unknown on line 0

Do you want to check an individual field’s size or the entire $_POST size?

If you’re trying to validate a file upload size, you can use http://www.yiiframew#maxSize-detail

If you wanna check the entire post’s size you can write a beforeValidate function (not tested)


public function beforeValidate()

{

	$isValid = false;

	$maxSize = 2048;

	

	$size = (int) $_SERVER['CONTENT_LENGTH'];

	

	if ($size < $maxSize)

	{

		$isValid = true;

	}

	else

	{

		// Not sure if this'll be cleared when you call validate but give it a whirl.

		$this->addError('chooseAFieldToAssignThisErrorTo', 'Your booty is too large!'); 

	}

	

	return $isValid;

}

But since this is a server limitation you probably looking for an application wide solution; not just for this model. In that case I need to give it some more thought. Will your server even let you hit your application if the post is too big or will it die on impact? Possibly onBeginRequest??

Matt

Hi!

The problem is that when you submit the for you are sending this file and the error appears before all validations. I am going to try your solution because is a single form app for a microsite. But I think i would be better a onBeginRequest solution…

Thanks!!!

Hi!

I have tested with BeginRequest but it seems that this error appears before that :(

I am totally out of ideas.

Is really possible manage the request before "Warning: POST Content-Length of bytes exceeds the limit of bytes in Unknown on line 0" appears??

I’m afraid there won’t be an easy solution to this. The warning will be thrown from within the early stages of PHP’s request parsing. You might be able to hide it by setting the error_level accordingly but it is hard to manage. Best you could do is informing your users beforehand of the size limitations for uploaded files.

What type of data are you sending via POST? Could you use client-side validation to limit it before you send it?

Hi guys!

Thank you for your responses :)

I agree with you guys but my client insists in this problem. I had never deal with this problem in other projects because the max size is set to 32M or more and then you check the size of the file in your app.

The client-side solution is the best in my oppinion but the customer ask me about what happen if user has not javascript…

I am going nuts! :)

Thank you again! and if I find a solution I’ll let you know about.

Hi guys!!!

We finally agree with set client-side limit validation this way:




               // On select image file

               $(".thePhoto").change(function(){

			if(this.files[0].size > 5242870)

			{

				alert("La foto debe ser inferior a 5MB editala y vuelve a intentarlo.");

			    $("#ytGraCompetitor_photo").val('');

			    $(".thePhoto").val('');

			}

			else

			{

				$("#filePhoto").html($(".thePhoto").val());

			}

		});


               // On sumbit form

               $(".btnForm").click(function(e){

                        var file = document.getElementById('GraCompetitor_photo').files[0];


                        if(file && file.size < 5242870) { // 5 MB (this size is in bytes)

                                //Submit form

                                $("#competitor-form").submit();       

                         } else {

                                //Prevent default and display error

                                e.preventDefault();

                                alert("La foto debe ser inferior a 5MB editala y vuelve a intentarlo.")

                         }			

                });