views:

643

answers:

6

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.

A: 

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.

Space
He wants to check the file size **before** uploading
Nifle
Yes I want to check file before uploading. Any help?
zeina
why negitive? Better to add comment so that the people know the reason.
Space
+2  A: 

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.

hpavc
He wants to check the file size **before** uploading.
Nifle
@Nifle and I want to travel in time.
Sinan Ünür
@Sinan - didn't you already finish your Time::Machine module?
DVK
@DVK Only Damian Conway knows how to write such a module. ;-)
Sinan Ünür
+1  A: 

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.

Nifle
You mean while reading the file?
zeina
+2  A: 

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

friedo
From `CGI.pm`: `if (($POST_MAX > 0) last METHOD; }`
Sinan Ünür
$ENV{CONTENT_LENGTH} seems to be reading the posted input size, yet it doesn't include file size. I tried it and it returned values less than the file size.
zeina
+1  A: 

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: http://stackoverflow.com/questions/207298/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.

Gavin Brock
A: 

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.

zeina