简体   繁体   English

文件上载大小验证

[英]File Upload Size Validation

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. 验证是一个大问题,就好像我在php中验证它具有我需要使其工作的所有功能..但它首先在临时位置上传文件,然后检查,这很糟糕。 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. 如果有人上传了一个100mb的文件,他必须等待时间只是为了得到没有错误,但只是一些内部的PHP搞砸了挂起页面。

One Way: JS 单程: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. 这适用于firefox,safari,chrome我猜也是,不是:在歌剧中,非常确定IE也可以通过ActiveX文件大小来处理IE但是Opera仍然卡住了。 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. 第二:我在想,我可以通过在php.ini中设置最大尺寸或类似的东西来提供php自定义警报或其他东西,这可以轻松解决问题。 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. 我一直在用电子邮件搞砸了解最新情况,我意识到即使他们使用javascript文件大小检查:P现在它与firefox很好用,其他像我说的,甚至IE,因为它有一个后退ActiveX方法但Opera是受害者:P他们不能在这种情况下给出一个奇特的javascript错误。 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. 一些内部php最大大小增加了错误,我可以自定义或运行一些脚本,以确定是否已达到最大大小或发现超过POST或类似的东西。 // 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. 希望一些服务器人可以帮助:)我认为这将解决它,如果有人这里的PHP服务器大师。

Thanks AND Regards. 谢谢并恭祝安康。 Please help. 请帮忙。 :) :)

Here is a plugin that uses flash + javascript (MooTools) to do file upload. 这是一个使用flash + javascript(MooTools)进行文件上传的插件。 The upside to this plugin is that it's supported and you can Github it. 这个插件的好处是支持它,你可以Github它。 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. 此外,还有使用PHP的后端示例,说明文件在上传后的处理方式。

Fancyupload 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 平台和服务器无关,只需要Flash9 +(> 95%渗透率),适用于安装它的所有浏览器

Here is the jQuery plugin that does the same as the MooTools one: 这是与MooTools一样的jQuery插件:

Uploadify Uploadify

You can set the max size allowed in PHP, Javascript & Apache. 您可以设置PHP,Javascript和Apache中允许的最大大小。

Use Javascript for the most user friendly, since it alerts the user immediately. 使用Javascript是最友好的用户,因为它会立即提醒用户。

Use PHP next to have an simple way to check after upload. 使用旁边的PHP有一个简单的方法来上传后检查。

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. 最后使用Apache,因为它的级别相当低,我相信检查过大的文件(给用户提供一个很好的警报)这样比较困难。

You can use a server-side solution. 您可以使用服务器端解决方案。

Use $_SERVER['CONTENT_LENGTH'] after trying to upload file. 尝试上传文件后使用$_SERVER['CONTENT_LENGTH'] 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) 即使filesize大于MAX_FILE_SIZEupload_max_filesizepost_max_size (来自php.ini),此方法也适用于上传文件时的所有错误情况。

For more features you can try http://valums.com/ajax-upload/ 有关更多功能,请尝试http://valums.com/ajax-upload/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM