Jump to content

Recommended Posts

Is it possible to check the filesize when a user uploads a file - for instance let says i want a form to only accept *.jpg, *.gif. *.zip and be a maximum of 2MB - how could this be achieved in javascript. If the file upload is greater than 2mb or isnt one of the accepted extensions i'd like an alert box presented to the user. Can anyone assist me :)

(or even in PHP if this isnt possible using javascript alone)

Thanks in advance :)
You cant with javascript however you can with PHP using this:

$_FILES['fileUploadFormNameHere'][size']

change fileUploadFormNameHere to the name you have named your upload form to. The above code returns the size of the file in bytes. To get the size in Megabytes you'll want to divide the size by 1024.

Have a read through [a href=\"http://uk2.php.net/manual/en/features.file-upload.php\" target=\"_blank\"]here[/a] on how to use the $_FILES superglobal array.

So to check the file size of an uploaded file you'll want to do this:
[code]$size = $_FILES['fileUploadFormNameHere'][size'];

if($size > 2097152)
{
    die('File size must not be over 2MB! Your file was ' . number_format(($size/1024/1024), 2) . 'MB');
}[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.