playaz Posted April 3, 2006 Share Posted April 3, 2006 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/6462-checking-filesize-on-upload-using-javascript/ Share on other sites More sharing options...
wildteen88 Posted April 3, 2006 Share Posted April 3, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/6462-checking-filesize-on-upload-using-javascript/#findComment-23429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.