在处理上传请求之前,如何在 Perl 中获取文件大小?

发布于 2024-08-29 21:33:55 字数 209 浏览 11 评论 0原文

我想获取文件大小我正在这样做:

my $filename=$query->param("upload_file");
my $filesize = (-s $filename);
print "Size: $filesize ";`

但它不起作用。请注意,我没有上传文件。我想在上传之前检查它的大小。因此将其限制为最大 1 MB。

I want to get file size I'm doing this:

my $filename=$query->param("upload_file");
my $filesize = (-s $filename);
print "Size: $filesize ";`

Yet it is not working. Note that I did not upload the file. I want to check its size before uploading it. So to limit it to max of 1 MB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

墨小沫ゞ 2024-09-05 21:33:55

在上传之前您无法知道某物的大小。但您可以检查浏览器发送的 Content-Length 请求标头(如果有)。然后,你可以决定是否相信它。请注意,Content-Length 将是整个请求流的长度,包括其他表单字段,而不仅仅是文件上传本身。但这足以让您获得一致客户的大概数字。

由于您似乎在普通 CGI 下运行,因此您应该能够在 $ENV{CONTENT_LENGTH} 中获取请求正文长度。

You can't know the size of something before uploading. But you can check the Content-Length request header sent by the browser, if there is one. Then, you can decide whether or not you want to believe it. Note that the Content-Length will be the length of the entire request stream, including other form fields, and not just the file upload itself. But it's sufficient to get you a ballpark figure for conformant clients.

Since you seem to be running under plain CGI, you should be able to get the request body length in $ENV{CONTENT_LENGTH}.

A君 2024-09-05 21:33:55

还想对可能已经设置了最大帖子数进行健全性检查(来自 perldoc CGI):

$CGI::POST_MAX

如果设置为非负整数,则此变量会限制大小
发帖数,以字节为单位。如果 CGI.pm 检测到大于上限的 POST,
它将立即退出并显示错误消息。该值将影响两者
普通 POST 和多部分 POST,这意味着它限制了最大大小
文件上传也是如此。您应该将其设置为一个相当高的值,例如
1 兆字节。

Also want to sanity check against possibly already having post max set (from perldoc CGI):

$CGI::POST_MAX

If set to a non-negative integer, this variable puts a ceiling on the size of
POSTings, in bytes. If CGI.pm detects a POST that is greater than the ceiling,
it will immediately exit with an error message. This value will affect both
ordinary POSTs and multipart POSTs, meaning that it limits the maximum size of
file uploads as well. You should set this to a reasonably high value, such as
1 megabyte.

多像笑话 2024-09-05 21:33:55

提交表单时,上传的文件会保存在服务器上的 tmp 位置,请检查那里的文件大小。

提供 $field 的值。

my $upload_filehandle = $query->upload($field);
my $tmpfilename = $query->tmpFileName($upload_filehandle);
my $file_size = (-s $tmpfilename);

The uploaded file is stashed in a tmp location on the server when the form is submitted, check the file size there.

Supply the value for $field.

my $upload_filehandle = $query->upload($field);
my $tmpfilename = $query->tmpFileName($upload_filehandle);
my $file_size = (-s $tmpfilename);
笑忘罢 2024-09-05 21:33:55

这与 Perl 无关。

您正在尝试使用读取服务器上文件的命令来读取用户计算机上文件的文件大小,但使用 Perl 无法完成您想要的操作。

这是必须在浏览器中完成的事情,简单地看一下这些问题,它要么非常困难,要么不可能。

最好的办法是允许用户开始上传并在文件太大时中止。

This has nothing to do with Perl.

You are trying to read the filesize of a file on the user's computer using commands that read files on your server, what you want can't be done using Perl.

This is something that has to be done in the browser, and looking briefly at these questions it's either very hard or impossible.

Your best bet is to allow the user to start the upload and abort if the file is too big.

素手挽清风 2024-09-05 21:33:55

如果您想在处理请求之前进行检查,最好检查触发请求的网页。我不认为网络浏览器可以自己做到这一点,但如果您不介意 Flash,有许多 Flash 上传工具可以检查大小(以及文件类型)等内容并阻止上传。

YUI Uploader 是一个不错的起点。这里有更多信息: 什么是最好的多文件 JavaScript / Flash 文件上传器?

显然您也想在服务器端进行检查,但是当用户开始向服务器发送请求时,您已经耗尽了 CPU 周期和带宽。

If you want to check before you process the request, you might be better off checking on the web page that triggers the request. I don't think the web browser can do it on it's own, but if you don't mind Flash, there are many Flash upload tools that can check things like size (as well as file types) and prevent uploading.

A good one to start with is the YUI Uploader. Lots more here: What is the best multiple file JavaScript / Flash file uploader?

Obviously you would want to check on the server side too, but by the time the user has started sending the request to the server, you are already using up your CPU cycles and bandwidth.

活雷疯 2024-09-05 21:33:55

谢谢大家的回复;我刚刚发现为什么 $filesize = (-s $filename); 之前不起作用,这是因为我在发送 Ajax 请求时检查文件大小,而不是在重新提交页面时检查文件大小。这就是为什么我的尺寸为零。我修复了该问题以提交页面并且它起作用了。谢谢。

Thanks everyone for your replies; I just found out why $filesize = (-s $filename); was not working before, it is due that I was checking file size while sending Ajax request and not while re submitting the page.That's why I was having size to be zero. I fixed that to submit the page and it worked. Thanks.

七月上 2024-09-05 21:33:55

只需阅读这篇文章,但在检查内容长度时是一个很好的近似预检查,您也可以将文件保存到临时文件夹,然后对其执行任何类型的检查。如果它不符合您的标准,只需将其删除,不要将其发送到最终目的地。

Just read this post but while checking the content-length is a good approximate pre-check you could also save the file to temporary folder and then perform any kind of check on it. If it doesn't meet your criteria just delete and don't send it to it's final destination.

放肆 2024-09-05 21:33:55

查看 perl 文档以获取文件统计信息 -X - perldoc.perl.orgstat-perldoc.perl.org。另外,您可以查看这个上传脚本,它正在执行类似的操作做你想做的事情。

Look at the perl documentation for file stats -X - perldoc.perl.org and stat-perldoc.perl.org. Also, you can look at this upload script which is doing the similar thing what you are trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文