SPUG: File headers

Darren/Torin/Who Ever... torin at daft.com
Wed Feb 9 08:04:04 CST 2000

Steve Laybourn, in an immanent manifestation of deity, wrote:
>   I'm wondering if there is a way to be able to open a remote file long 
>enough to get the first 256 bytes and close the record again.
>   If I wanted to scan the first 1024 bytes of
>to make sure it WAS a JPEG file without having to load the entire image, 
>could it be done? If so, how? open / read don't seem to do it.

Yes, you can use the Ranges header in HTTP to request only a certain
amount.  The http server that you talk to must support HTTP/1.1 and
support byte-ranges but most of them do, so that's okay.  Note that to
comply with HTTP/1.1

BTW, you want to use example.com as an example instead of other
hostnames.  www.arglebargle.com exists but there is no images
directory.  :)  example.com is specifically reserved for use in examples
and will never be allocated to anyone.

So, you could do the following:
(use !perl -x on this message to run it.)
#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use Image::Size;

my $ua  = LWP::UserAgent->new;
my $res = $ua->request(GET 'http://www.pm.org/Images/tshirt.jpg',
		       Range      => 'bytes=0-1023',
		       Connection => 'Close',
die "Unable to retrieve image: " . $res->status_line unless $res->is_success;

# the third element of imgsize's return is the file type. i.e. JPG
print +(imgsize($res->content_ref))[2], "\n";

<torin at daft.com> <http://www.daft.com/~torin> <torin at debian.org> <torin at io.com>
Darren Stalder/2608 Second Ave, @282/Seattle, WA 98121-1212/USA/+1-800-921-4996
@ Sysadmin, webweaver, postmaster for hire. C/Perl/CGI/Pilot programmer/tutor @
@		     Make a little hot-tub in your soul.		      @

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address

More information about the spug-list mailing list