How to add file size limits to Upload Form

How do I add a file size limit to my upload form code (below)?

Is this correct?
if ( $_FILES[“file”][“size”] < 100000

Do I need any additional code to make that line of code functional?
Where can I add it into my code to make it functional?

$allowedExts = array("gif", "jpeg", "jpg", "pdf", "doc", "docx", "txt", "rtf", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = strtolower( end($temp) );
if (!in_array($extension,$allowedExts))
echo ("Error - Invalid File Name");
$length = 20;
$randomString = (time());
$thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $thumbnail);
$sql = "INSERT INTO videos ( filename ) VALUES( '$thumbnail' )";
$file_location = '<a href="' . $thumbnail . '">' . $thumbnail .     '</a>';

Thanks. I look forward to any help.

Your file size will only be limited by PHP max upload size from the ini file.
You can limit the size you accept the way you’re suggesting. Alternatively you can use the filesize() function. If you tell your users 100k is the max, then be fair and compare with 100*1024.
Be sure to notify your users correctly when you reject the upload.

Edit php.ini file

Change below param as per your need

file_uploads = On
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300s
memory_limit = 128M

file_uploads = To allow HTTP file uploads.
upload_max_filesize = Maximum allowed size - uploaded files
max_execution_time = Maximum execution time for each script,( Seconds )
memory_limit = Maximum amount of memory a script may consume (128MB)
post_max_size = Maximum size of POST data that PHP will accept.Its value may be 0 to disable the limit.
It is ignored if POST data reading is disabled through enable_post_data_reading.

I suggest reading PHP’s manual on this. It details ways you can do this with HTML.

Not all browsers comply with file size limits in HTML

Also it is easy for people to change the HTML so as to bypass any limit specified there.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.