开发者

How can I get file size in Perl before processing an upload request?

开发者 https://www.devze.com 2022-12-27 23:12 出处:网络
I want to get file size I\'m doing this: my $filename=$query->param(\"upload_file\"); my $filesize = (-s $filename);

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. No开发者_开发技巧te 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.


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}.


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.


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);


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.


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消