Solved

Uploading

Posted on 2000-02-22
10
224 Views
Last Modified: 2010-03-05

While uploading a file from a form (using "file" type) can I get the size of the file ? Or perhaps the size that has been uploaded so far?
0
Comment
Question by:leoleo
  • 4
  • 3
  • 3
10 Comments
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
yes you can..

try this...


=============upload.html==============

<form ENCTYPE="multipart/form-data" action="/cgi-bin/upload.pl" Method=POST>
Attach:<input name="doc" type="file">
<INPUT TYPE=SUBMIT>
</form>

==================upload.pl
#!/usr/local/bin/perl

use CGI;

$query=new CGI;

$DocLocation=$query->param('doc');


$WebFileName = "UpdateGroupFile.htm";
print "Content-type: text/html\n\n";

`rm /tmp/$WebFileName`;
open(MYFILE,"> /tmp/$WebFileName") || die $!;
binmode MYFILE;
while($bytesread=read($DocLocation,$data,1024)){
  $size+=$bytesread; ## Keep track of file size
  print MYFILE $data;
}
close(MYFILE);
close($DocLocation);

`chmod 777 /tmp/$WebFileName`;

print "<B>$DocLocation has been uploaded</B><br>\n";
print "<B>Size = ",$size," byte(s)</B><BR>\n";
0
 
LVL 3

Expert Comment

by:alien_life_form
Comment Utility
Grretings.

Maneshr, your code is correct, but, what you get is actually the size *after* the file has already been uploaded (by CGI.pm) and saved to temporary file.

So, you can get at the size of the file
with:

$size=(stat($query->tmpFileName($query->param('doc')))[7];

OTOH, getting to know the size *before* uploading it, and limiting the upload, requires rummaging through the internals of CGI.pm, which goes out of its way to hide this kind of details. Gaining more control probably leads to overrriding CGI.pm's methods, and specifically the read_multipart sub (look for the UPLOADS: label).
Now that sorta breaks the incapsulation and code privacy stuff...;-)

Failing this, and if coarse grained control is sufficient, one can set the $CGI::POST_MAX variable  to some maximum byte value, and be assured that the *total* post size will not exceed that value.

As a final note, some clients make it possible to know the file size in the header of the multipart document (I.e., they set Content-Length on the single part). But that is client dependent and, last time I looked, netscape was not doing it, so it cannot really be relied upon.

Because of this, the only sure way to know if a file exceeds some specified length is to read that many bytes from stdin.

HTH & cheers,
   alf

0
 

Author Comment

by:leoleo
Comment Utility

Thanks, it worked.

One more thing: Is there absolutely no way of getting the total file size just as upload starts? So that I can specify what % has been uploaded so far?
0
 

Author Comment

by:leoleo
Comment Utility

Thanks, it worked.

One more thing: Is there absolutely no way of getting the total file size just as upload starts? So that I can specify what % has been uploaded so far?
0
 
LVL 3

Expert Comment

by:alien_life_form
Comment Utility
See my comment above.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:maneshr
Comment Utility
alf,

you are not totally accurate when you say "...what you get is actually the size *after* the file has already been uploaded".

you get the size of the file even as it is being read and not after the file has been uploaded. so if you want to abort the download after reaching a particular limit you can still do that and not have the entire file.

However, in this case i think leoleo would be better off using the solution you have provided.
0
 
LVL 3

Accepted Solution

by:
alien_life_form earned 30 total points
Comment Utility
Greeting.

Well, the way CGI.pm does uploads reads, in part:

[...]
$tmpfile = new TempFile;
$tmp = $tmpfile->as_string;

$filehandle = Fh->new($filename,$tmp,$PRIVATE_TEMPFILES);

$CGI::DefaultClass->binmode($filehandle) if $CGI::needs_binmode;
chmod 0600,$tmp;    # only the owner can tamper with it

my ($data);
local($\) = '';
while (defined($data = $buffer->read)) {
    print $filehandle $data;
}
[...]

here, $buffer is an instance of MultipartBuffer which deep inside is little more than a filehandle. I can see no good way to get into the while (but I'll be glad to be shown one), except by perhaps overriding the read method in  MultipartBuffer - which may be preferable than overriding the whole shebang, anyway.

Looks to me that, by the time that the sub returns, the upload is over. Now it would be nice to have the possibility to supply a callback function to oversee the process, but Lincoln (Stein) will have to take care of that... :-)

Cheers,
  alf
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
leoleo, was your problem solved by the answers you got?

Pl. let  the forum know.
0
 

Author Comment

by:leoleo
Comment Utility

 I think so, I am still exploring the CGI module which I have not used much.

Thanks indeed.
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
Glad to know that you have your solution :). Please go ahead & award thte points to the right answer

Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now