Category: >> Perl Code >> Get File Size Over HTTP Bookmark and Share

<< lastnext >>

Snippet Name: Get File Size Over HTTP

Description: This snippet uses LWP::UserAgent to retrieve info about a remotely hosted file over HTTP.

Also see:
» HTTP Post With Perl

Comment: (none)

Author: CoderZone
Language: PERL
Highlight Mode: PERL
Last Modified: December 03rd, 2010

use LWP::UserAgent;
sub GetFileSize{
        my $url=shift;
        $ua = new LWP::UserAgent;
        my $req = new HTTP::Request 'HEAD' => $url;
        $req->header('Accept' => 'text/html');
        $res = $ua->request($req);
        if ($res->is_success) {
                 my $headers = $res->headers;
                 return $headers;
return 0;
$header = GetFileSize($link);
print "File size: ".$header->content_length." bytes\n";
print "Last moified: ".localtime($header->last_modified)."\n";
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© | users online: 8