Hi Daniweb Community,

I'm having a small problem. I am unable to get the size of an uploaded file from the server to run a max size check. When I use the following command as specified in any example of file upload I can find on the web:

if ($_FILES["r_recommend"]["size"] > 500000){
    echo "error text";
}else{
    move on to upload process
}

This, or any variation thereof, generates no response and the file happily uploads. If the file exceeds the server upload limit, it simply does nothing and returns to upload screen--no error is generated.

A few other things: my input tag does have the both the method="post" and the enctype="multipart/form-data" settings and file uploads are allowed in the php.ini file.

I have worked around this by setting the file upload limit to 25mb--which covers 99% of past uploads, but we would like to make the upload limit smaller to save server space, so I need to figure out a way to generate an error when the user attempts a very large upload.

Your assistance would be greatly appreciated.

Hi,

with browsers requests, the server can only accept the upload and check the sizes when it finishes. So, if you want to manage the error through PHP, raise the size and set a lower limit in the script.

If the limit in the php.ini file is 25MB and the server gets a bigger request body, then you will not see an error response. But the error log should return some information. The server can also handle the request by limiting the body request size and return an error.

You can check the file size with javascript, before the upload starts, see this snippet:

This will not stop malicious users, but it can be user-friendly solution.

<rant>
If browsers worked properly then they would wait for a HTTP/1.1 100 CONTINUE response from the server: the client in this case must send the file size through an header, wait for the server to validate the request and then proceed or discard. That would be wonderful to save bandwidth.
</rant>

commented: good link - bookmarked :) +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.