Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have a directory on a server somewhere, and I am trying to get all the .jpg images form there and save these images somewhere in my machine or it could be saved somewhere else. But I am having problems open this remote directory, I might be using the wrong Perl code to open this directory, any help?

$dir = "http://myserver.com/images"; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # only files next unless (-f "$dir/$file"); # find files ending in .JPG next unless ($file =~ m/\.JPG$/); print "$file\n"; # Here I need to send all found pictures to a location in my l +ocal directory. } closedir(DIR); exit 0;

Replies are listed 'Best First'.
Re: Open Remote Directory
by clinton (Priest) on May 09, 2007 at 16:10 UTC
Re: Open Remote Directory
by sgifford (Prior) on May 09, 2007 at 16:41 UTC
    The specific problem you're facing is that perl's opendir only works on local directories, and the more general one is that HTTP doesn't provide any standard way to get a listing of files in the directory. You will have to either parse the Web page (using something like HTML::TreeBuilder), or else access the remote directory with a different protocol (like FTP with Net:FTP, WebDAV with HTTP::DAV, or ssh with File::Remote).
Re: Open Remote Directory
by jasonk (Parson) on May 09, 2007 at 16:08 UTC

    I find it very unlikely that you have a directory named 'http://myserver.com/images', and as you seem to be aware, opendir works on DIRECTORIES, not URLs.


    We're not surrounded, we're in a target-rich environment!