Mortgage Insurance - Web Advertising - Refinance - 2008 NFL Draft Order - Homeowner Loan

PDA

View Full Version : Check file size before upload?


anton-io!
Dec 22nd 2004, 12:06 am
Looking for a client side script that will check the file size of a user selected file (i.e., image) before it is uploaded to the server.

Possible in Javascript?

Looking for a Javascript/PHP solution or a simple PHP solution if possible.

Thanks greatly!

nevetS
Dec 23rd 2004, 12:22 am
php is all server side scripting, so you are out of luck in that regard. Somebody correct me if I'm wrong, but there is no way to check a file size in php prior to upload. You can set a parameter in your php.ini file that I believe is MAX_UPLOAD or MAX_FILE_UPLOAD to 1 megabyte or another arbitrary number if you want. If someone tried to upload a larger file, they would get an error.

On the javascript/vbscript side, I believe that you are looking for information that is not available via those scripting languages either.

Your only option would be java if you wanted to check something client side.

I could be wrong, though. It's happened before (twice) :).

dustin
Dec 23rd 2004, 1:56 am
Java applets have a pretty strict security policy in place. It prevents them from doing nasty things to the computers of people who view them. So I'm not sure if they would be able to check an image's size.

relaxzoolander
Jan 15th 2005, 5:50 pm
i use an asp script that checks file size.
.

phrozen_ra
Jan 18th 2005, 1:26 am
http://ro.php.net/features.file-upload

with PHP and it's not that difficult... if you still don't understand i'll help you :)

RussellMD
Apr 10th 2008, 8:32 am
Hi!

You can use a server-side solution.

Use $_SERVER['CONTENT_LENGTH'] after trying to upload file. This variable returns size of uploaded file, even if upload failed


if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']>2097152)
$error = 'Upload FAILED, file is too large !';

This method work fine for all cases of errors when upload file , even if filesize is greater than MAX_FILE_SIZE, upload_max_filesize or post_max_size (from php.ini)

Good Luck! && Sorry for my bad english.

dineshw
Apr 11th 2008, 9:02 pm
RussellMD,

Wont that only check the file size AFTER the file has already been uploaded to the temporary folder? Doesnt that defeat the purpose?

Dinesh