Solved

Uploading

Posted on 2000-02-22
10
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2546159
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
ID: 2549578
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
ID: 2549582

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:leoleo
ID: 2549587

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
ID: 2549588
See my comment above.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2550475
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
ID: 2550581
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
ID: 2559384
leoleo, was your problem solved by the answers you got?

Pl. let  the forum know.
0
 

Author Comment

by:leoleo
ID: 2560333

 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
ID: 2561363
Glad to know that you have your solution :). Please go ahead & award thte points to the right answer

Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
collecting information 2 220
Perl script not writing to .csv file 6 125
Perl string replace for refred url 9 73
Merging two files with Perl 5 59
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

752 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