Humpty Posted April 24, 2007 Share Posted April 24, 2007 G'day JS guys, I am completely new to JS, (been using PHP for a while). I am creating a PHP site where end users are required to upload images. I need to ensure that the end user doesn't upload a file that too big. I can't find a way to set this limitation in the HTML, and checking it using PHP is too late as the file is already uploaded. Is there some JS that I can use to check the file size client-side? (It's also important that whatever JS I use, if any, doesn't contain Js scripts that will be a lot of dectors be considered spyware...I do realise that the end user can disable JS and also that they can alter settings of thier spyware software but if I can't cater for everyone I can at least cater for normal end users). Background: This is for uploading images of houses to a realestate site, a lot of people that use this (including realestate staff members wouldn't or don't resize images prior to uploading. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/ Share on other sites More sharing options...
fenway Posted April 24, 2007 Share Posted April 24, 2007 You can't do this in JS at all. Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/#findComment-237311 Share on other sites More sharing options...
Humpty Posted April 25, 2007 Author Share Posted April 25, 2007 bugger hey? Well thanks for that anyway. I guess the end users who aren't bright enough to resize an image prior to uploading will just have to wait while thier huge 2MB+ files are being uploaded (possibly ten of them if they choose to). Appreciate the response all the same though, far better than not getting a response. - Humpty Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/#findComment-238019 Share on other sites More sharing options...
Humpty Posted April 25, 2007 Author Share Posted April 25, 2007 OK: Now although fenway was correct in that there is no JS to check this there are other steps we can take. (mods: please don't kill this thread or post due to the below, because future forum readers may be looking for the same thing and not be able to find a solution elsewhere, it is possible that fenway knew of this but didn't mention it because I didn't ask for an alternative) The below is HTML and PHP specific. There is a an attribute to the input tag where the type is file which is "maxlength=xx" (xx being a size in bytes). This is reported as not support in most mainstream browsers. If you are using PHP as your server side you can a hidden input field which will limit the file size. This can be handy if you don't want the end user uploading a 10 MB file for example. (because that would take a long time) you can specify a file size of say 1MB (1000000 bytes) and although it would take a while for that to upload it's ten times shorter because that file will be cut off at 1 MB and the remaining 9 MB will not be uploaded. you can then use error checking to inform the user that thier file size was too big and that it should be less than 1MB (or the size that you specify) How to do it: Add the following tag BEFORE the files input tag: <input type="hidden" name="MAX_FILE_SIZE" value="1000000"/> I am unsure but presume that PHP would use the 1024 to 1 rule for file sizes (even though in the above example I used the 1000 to 1 rule). -Humpty Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/#findComment-238127 Share on other sites More sharing options...
fenway Posted April 25, 2007 Share Posted April 25, 2007 You don't need that at all -- you can hard-code in whatever server-side limit you want (and you should abort the upload after that). Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/#findComment-238577 Share on other sites More sharing options...
Humpty Posted April 26, 2007 Author Share Posted April 26, 2007 Yes but doesn't that mean you have to edit the ini file? A lot of peole don't have access to these things, they just buy the hosting, use it as required and often don't bother with trying to get the host to change things (I can only guess that a better host will be more accomodating for that sort of thing) Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/#findComment-238845 Share on other sites More sharing options...
fenway Posted April 26, 2007 Share Posted April 26, 2007 Not the PHP ini, the PHP script that's handling the upload. Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/#findComment-238983 Share on other sites More sharing options...
Humpty Posted April 26, 2007 Author Share Posted April 26, 2007 seriously? I realise that this is a JS forum but we've alrady started so.... can i grab a push in the right direction? Thanks - humpty Link to comment https://forums.phpfreaks.com/topic/48448-solved-input-typefile-check-the-file-size-before-upload/#findComment-239457 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.