上传前检查文件大小 [英] Checking File Size Before Upload

查看:70
本文介绍了上传前检查文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我不是开发人员,但是我和几个人一起工作,并且发布了这个用于

他们。我们正试图确定在用户上传之前是否可以检查文件的大小。


我读过的所有内容都表明这在PHP中是不可能的(或者至少非常难以理解),这对于服务器端语言来说是有意义的。

然而,让我暂停的是事实当用户试图上传一个大于我们允许的最大文件大小的文件时,我们的错误日志中会立即出现错误

- 但上传仍在继续。 br />

所以不管怎样,PHP必须承认该文件太大了

它已被上传。问题是如何 - 以及我们如何从认可中立即停止上传?

Hi all,

I am not a developer, but I work with several and am posting this for
them. We are trying to determine whether it is possible to check the
size of a file before a user uploads it.

Everything I''ve read suggests that this is impossible (or at least very
difficult) in PHP, and this makes sense for a server-side language.
What gives me pause, however, is the fact that when a user tries to
upload a file larger than the max file size we allow, an error appears
in our error log immediately -- yet the upload continues.

So somehow PHP must be recognizing that the file is too big well before
it''s been uploaded. The question is how -- and how do we make the leap
from that recognition to an immediate halt of the upload?

推荐答案

2006年8月22日14:59:35 -0700,jeremyz < je ********* @ gmail.comwrote:
On 22 Aug 2006 14:59:35 -0700, "jeremyz" <je*********@gmail.comwrote:

>我不是开发人员,但我和几个人合作并发布了这个对于他们。我们正试图确定在用户上传之前是否可以检查文件的大小。

我读过的所有内容都表明这是不可能的(或者至少非常
困难)在PHP中,这对于服务器端语言是有意义的。
然而,让我暂停的是,当用户试图上传大于我们允许的最大文件大小,我们的错误日志中会立即显示一个错误 - 但上传仍在继续。

因此,某种程度上PHP必须认识到该文件太大了
它已被上传。问题是如何 - 以及我们如何从这种认识中跳跃到立即停止上传?
>I am not a developer, but I work with several and am posting this for
them. We are trying to determine whether it is possible to check the
size of a file before a user uploads it.

Everything I''ve read suggests that this is impossible (or at least very
difficult) in PHP, and this makes sense for a server-side language.
What gives me pause, however, is the fact that when a user tries to
upload a file larger than the max file size we allow, an error appears
in our error log immediately -- yet the upload continues.

So somehow PHP must be recognizing that the file is too big well before
it''s been uploaded. The question is how -- and how do we make the leap
from that recognition to an immediate halt of the upload?



浏览器可以通过文件上传发送Content-length标头。 PHP可以立即读取
,因为它在请求开始时是正确的,所以如果它也是如此,那么
会在日志中产生错误很大。


但是,PHP还没有办法向浏览器发送响应,因为

文件的内容属于HTTP请求。它的选择是要么在没有响应的情况下毫不客气地转储连接(它不会这样做,

,原因很明显,尽管HTTP协议确实允许这样做),或者等到

请求已经完成,所以它可以发回一条带有错误消息的回复。


AFAIK这是HTTP协议的限制,所以不能工作

左右。


-

Andy Hassall :: an ** @ andyh.co.uk :: http://www.andyh.co.uk
http ://www.andyhsoftware.co.uk/space ::磁盘和FTP使用情况分析工具

The browser may send a Content-length header with the file upload. PHP can
read this immediately, since it''s right at the start of the request, and so can
produce an error in the log if it''s too big.

However, there''s no way for PHP to send a response to the browser yet, because
the content of the file is part of the HTTP request. Its choices are to either
unceremoniously dump the connection without a response (which it doesn''t do,
for obvious reasons, although the HTTP protocol does allow this), or wait until
the request has finished so it can send a response back with an error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool


Andy Hassall写道:
Andy Hassall wrote:

但是,PHP还没有办法向浏览器发送响应,因为

文件的内容是o的一部分o f HTTP请求。它的选择是要么在没有响应的情况下毫不客气地转储连接(它不会这样做,

,原因很明显,尽管HTTP协议确实允许这样做),或者等到

请求已经完成,所以它可以发回一条带有错误消息的回复。


AFAIK这是HTTP协议的限制,所以无法工作

左右。
However, there''s no way for PHP to send a response to the browser yet, because
the content of the file is part of the HTTP request. Its choices are to either
unceremoniously dump the connection without a response (which it doesn''t do,
for obvious reasons, although the HTTP protocol does allow this), or wait until
the request has finished so it can send a response back with an error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.



我认为HTTP协议规范要求在完全接收请求主体后响应只能发送
。理论上,

服务器可以发送状态代码100来接受请求或4xx错误

代码来拒绝它。这不是它在浏览器中实现的方式

虽然,AFAIK。


浏览器应该将请求的大小传递给onSubmit if你问

我。有时我希望HTML并没有完全停止进展...

I don''t think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received. In theory, the
server can send status code 100 to accept the request or a 4xx error
code to reject it. That''s not how it''s implemented in the browsers
though, AFAIK.

The browser should pass the size of the request to onSubmit if you ask
me. Sometimes I wish HTML hadn''t just stopped progressing completely...


Andy Hassall写道:
Andy Hassall wrote:

但是,PHP还没有办法向浏览器发送响应,因为

文件的内容是HTTP请求的一部分。它的选择是要么在没有响应的情况下毫不客气地转储连接(它不会这样做,

,原因很明显,尽管HTTP协议确实允许这样做),或者等到

请求已经完成,所以它可以发回一条带有错误消息的回复。


AFAIK这是HTTP协议的限制,所以无法工作

左右。
However, there''s no way for PHP to send a response to the browser yet, because
the content of the file is part of the HTTP request. Its choices are to either
unceremoniously dump the connection without a response (which it doesn''t do,
for obvious reasons, although the HTTP protocol does allow this), or wait until
the request has finished so it can send a response back with an error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.



我认为HTTP协议规范要求在完全接收请求主体后响应只能发送
。理论上,

服务器可以发送状态代码100来接受请求或4xx错误

代码来拒绝它。这不是它在浏览器中实现的方式

虽然,AFAIK。


浏览器应该将请求的大小传递给onSubmit if你问

我。有时候我希望HTML并没有完全停止进展......

I don''t think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received. In theory, the
server can send status code 100 to accept the request or a 4xx error
code to reject it. That''s not how it''s implemented in the browsers
though, AFAIK.

The browser should pass the size of the request to onSubmit if you ask
me. Sometimes I wish HTML hadn''t just stopped progressing completely...


这篇关于上传前检查文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆