File Upload Size Validation

2020-07-13 10:21发布

Validations are a big problem, as if i validate in php it has all the functions etc i need to make it work.. But it uploads the file first on a temporary location and then checks, which sucks. If someone uploads a file of 100mb he has to wait for the time just to get no error, but just some internal php screw up hanging the page.

One Way: JS

//file is id of input file
alert(document.getElementById('file').files[0].fileSize);

This works in firefox, safari, chrome i guess too, NOT: In opera, quite sure IE too but IE can be taken care of by ActiveX file size but Opera i am still stuck. So pretty unusable, anyways to get around it?

Second: I was thinking, can i give a php custom alert or something by setting max size in php.ini or something like that, that could easily solve the problem. Thats what i am looking for.

ANOTHER UPDATE:

I have been fooling around with rapidshare to understand whats going on, i realized even they use javascript file size checking :P Now that works great with firefox, and the others like i said, even IE as it has a fall back ActiveX method but Opera is the victim :P They cant give a fancy javascript error in that case. But they do have a fall back server side validation takes a few seconds more, is not smooth but it does show a small error finally.

So just need to find out that server side part now without uploading, and in my mind only one way to do it:

  1. Some Internal php max size increased error which i can customize or run some script to find out whether max size has been reached or find POST exceeded or anything like that. // NO IDEA ABOUT THIS. Hopefully some server guy can help :) I think this is what will solve it if anyone a php server guru here.

Thanks AND Regards. Please help. :)

3条回答
狗以群分
2楼-- · 2020-07-13 11:11

Here is a plugin that uses flash + javascript (MooTools) to do file upload. The upside to this plugin is that it's supported and you can Github it. It can limit max size, etc, and verify file information before upload. Also, has example for backend using PHP on how files are handled after it is uploaded.

Fancyupload

Features

  • Select and upload multiple files
  • Filter files by type in the select
  • dialog A lot of possible Events to add your own behaviour
  • Show and filter useful file information before the upload starts
  • Limit uploads by file count, type or size
  • Platform and server independent, just needs Flash9+ (> 95% penetration) which works on all browsers with it installed

Here is the jQuery plugin that does the same as the MooTools one:

Uploadify

查看更多
混吃等死
3楼-- · 2020-07-13 11:17

You can set the max size allowed in PHP, Javascript & Apache.

Use Javascript for the most user friendly, since it alerts the user immediately.

Use PHP next to have an simple way to check after upload.

Use Apache last since it's pretty low level and I believe checking for too large of a file (to give a nice alert to the user) is more difficult this way.

查看更多
▲ chillily
4楼-- · 2020-07-13 11:17

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)

For more features you can try http://valums.com/ajax-upload/

查看更多
登录 后发表回答