Perl, resolving link download name
Pregunta
I am trying to write a script in Perl that generates URL list from web page, than goes over URL list and downloads the linked files.
the problem is that the generated URL's links look something like
www.example.com/download.php?file=Li9OaW50ZW5kby9ORVMvVVNBLzEwIFlhcmQgRmlnaHQuemlw
when i place this link in browser it knows the true file name (i.e. cards.zip), i don't know how to get it using Perl.
how can i get using Perl the link's 'true' file name ?
Solución
Inspect the headers, specifically Content-Disposition
.
Otros consejos
Amadan had it right, just showing the code i used to solve it:
use LWP::UserAgent qw( );
use LWP::Simple;
$url = 'http://www.example.com/download.php?file=Li9OaW50ZW5kby9ORVMvVVNBLzNEIEJhdHRsZXMgb2YgV29ybGQgUnVubmVyLnppcA==';
#get file name
my $ua = LWP::UserAgent->new();
$cnt = %{%{$ua->head( $url )}->{'_headers'}}->{'content-disposition'};
$cnt =~ m/filename=(.*)/;
print "File name is: $1\n";
#Save File
my $status = getstore($url, $1);
if ( is_success($status) ){
print "File $1 Saved Correctly !\n";
}else{
print "Error saving $1!\n";
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow