CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Check if file exists with only URL

 



mmcw
User

Jun 21, 2001, 1:08 AM

Post #1 of 8 (1169 views)
Check if file exists with only URL Can't Post

How to check if a file exists when I do only have the URL! I do not have the path to the file.

With the path I know you can use: -e $file

Michel




mhx
Enthusiast / Moderator

Jun 21, 2001, 9:26 AM

Post #2 of 8 (1163 views)
Re: Check if file exists with only URL [In reply to] Can't Post

Hi Michel,

use the get function from the LWP::Simple module:

Code
#!/bin/perl -w 
use strict;
use LWP::Simple;

my $file = 'http://www.somewhere.org/path/to/my/file';

if( defined get( $file ) ) {
print "File $file exists!\n";
}
else {
print "File $file doesn't exist!\n";
}

If the return value of get is defined, it is the file you requested. So you could extend this and say:

Code
#!/bin/perl -w 
use strict;
use LWP::Simple;

my $file = 'http://www.somewhere.org/path/to/my/file';
my $data = get( $file );

if( defined $data ) {
print "File $file exists! Here it is:\n$data";
}
else {
print "File $file doesn't exist!\n";
}

Hope this helps.

-- Marcus



Mortimer
journeyman

Jun 21, 2001, 11:50 PM

Post #3 of 8 (1155 views)
Re: Check if file exists with only URL [In reply to] Can't Post

That's not good enough because .htaccess will make your script report an existing file as non existent.
Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com




mhx
Enthusiast / Moderator

Jun 22, 2001, 2:51 AM

Post #4 of 8 (1152 views)
Re: Check if file exists with only URL [In reply to] Can't Post

Hi Dave,

you're absolutely right. Do you have any idea how to get around this problem?

-- Marcus



Mortimer
journeyman

Jun 23, 2001, 8:27 AM

Post #5 of 8 (1143 views)
Re: Check if file exists with only URL [In reply to] Can't Post

Hello Marcus.
Well I wouldn't call it a problem, and you can't get around it (nicely), you have to be authorized for this. You can have the LWP::UserAgent module's authorization_basic() method verify that you're authorized to mess with a file...


Code
use LWP::UserAgent; 

my $locf = "/path/to/local/file.ext";
my $f = 'http://www.domain.com/path/to/serverfile.ext';
my $r = new HTTP::Request 'GET', $f;
my $ua = new LWP::UserAgent;
$r->authorization_basic( 'username', 'password' );

my @lines = map{ $_ } split ($/), $ua->request( $r )->content;
#$ua->mirror( $f, $locf ); # If accessing non .htaccess dirs
open( FILE, ">$locf" )or die( "Can't open $locf: $!" );
print FILE @lines;
close( FILE );

If you're not authorized, the server will tell you.
If you are authorized and the file doesn't exist, you'll just get the 404 Not Found message.

For some reason we can't mirror() the files in .htaccess protected directories even if we're authorized.
If we want to save the data, we have to create a file with open().

Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com



mhx
Enthusiast / Moderator

Jun 23, 2001, 8:42 AM

Post #6 of 8 (1141 views)
Re: Check if file exists with only URL [In reply to] Can't Post

Thanks for explaining this to me! I'm glad my solution wasn't so wrong. One just has to keep in mind under which circumstances it might fail.

-- Marcus



mmcw
User

Jul 13, 2001, 10:50 PM

Post #7 of 8 (1117 views)
Re: Check if file exists with only URL [In reply to] Can't Post

This solution will only work if the file is not on a secure server!! What when the file is on a secure server??



mhx
Enthusiast / Moderator

Oct 12, 2001, 10:37 PM

Post #8 of 8 (1092 views)
Re: Check if file exists with only URL [In reply to] Can't Post

All LWP modules will work fine with SSL if you install Crypt::SSLeay.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives