Link to home
Start Free TrialLog in
Avatar of mmcw
mmcw

asked on

Check if file exists

Is it possible to check if a file in a specified dir exists.

Path to dir: http://www.test.com/images
$file = "test.gif";

The file I want to check for are only gif and jpg files.

When the file exist, the value
$exist = 1;
else the value
$exist = 0;
Avatar of mmcw
mmcw

ASKER

Edited text of question.
You're going to have to use something like HTTP::Request

require HTTP::Request;
$request = HTTP::Request->new(GET => 'http://www.test.com/images/test.gif');

Most likely you'll need the libwww bundle of modules

Good luck
ASKER CERTIFIED SOLUTION
Avatar of khacharn
khacharn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmcw

ASKER

Where do I get more information about HTTP::request???
YOu can get the information by downloading the module and installing it and then issuing perldoc HTTP::Request

It also might be worthwhile to look at LWP::Simple. This module has a simplified way to do web stuff.

All modules are available at:
http://www.perl.com/CPAN-local/modules/01modules.index.html

good luck
Remember to reject the other answer if you find that mine is more apropriate.
Cheers
M
Well well..
mmcw..
i hope you try my suggestion too..
i feel that it will slove your problem easily..
nitin
Regards
'-e $file' works if the file is on the local machine. Does not work for URLs (as I understand the latter is what mmcw is trying to do).

HTTP::Request lets you construct the Request object, but it does not send the request.

For URLs, there are several choices:
- HTTP::Request (in combination with LWP::UserAgent)
- HTTP::Request::Common (in combination with LWP::UserAgent)
- LWP::Simple

Although I prefer the last one for simple things.

Also, if you want to check for the existence of the URL only, the HEAD method is more appropriate than GET. HEAD just gets the headers while GET gets the whole page.

use LWP::Simple

$exist = head($url) ? 1 : 0;


## OR

use HTTP::Request;
use LWP::UserAgent;

$request = HTTP::Request->new(HEAD => $url);
$ua = LWP::UserAgent->new;
$response = $ua->request($request);
$exist = $response->is_success ? 1 : 0;


## OR

use HTTP::Request::Common;
use LWP::UserAgent;

$ua = LWP::UserAgent->new;
$response = $ua->request(HEAD $url);
$exist = $response->is_success ? 1 : 0;
good answer
Avatar of mmcw

ASKER

Question to prakashk:

That is what I mean!!
A question:
My provider did not install the LWP::simple module.
Is it possible to use or make it for myself??

greetings Michel

p.s.: No LWP modules are installed!
You can install the modules in your own area. Do you have telnet access to the system?

Please note that LWP::Simple is part of the LWP bundle. You cannot just install this module only and expect it to work. It depends on other modules in the bundle. However, it is not difficult to install the bundle, if you have sufficient disk space in your area on the system and command-line shell access.
Avatar of mmcw

ASKER

I do not have.
Is there not another way??
## the following works only for 'http' requests on port 80

use Socket;

$url = shift;
($method, $path) = split '://', $url;
($host, $path) = split '/', $path, 2;

$proto = getprotobyname('tcp');
socket(SH, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";

$iaddr = gethostbyname($host);
$port = getservbyname('http', 'tcp') || 80;
$sin = sockaddr_in($port, $iaddr);
connect(SH, $sin) or die "connect: $!\n";

$path ||= '/';
$sent = send(SH, "HEAD $path HTTP/1.0\n\n", 0, $sin) or die "send: $!";

if (recv(SH, $buf, 1024, 0)) {
    ($x, $result) = split /\s+/, $buf;
    $exist = ($result == 200) ? 1 : 0;
} else {
    die "recv: $!";
}

print "exist = $exist\n";

close SH;
Avatar of mmcw

ASKER

Hello,

I tried to use this but with no result!!

$url = "$imgurl/$r_rowdata->{$_}";

use Socket;
#$url = shift;
($method, $path) = split '://', $url;
($host, $path) = split '/', $path, 2;

$proto = getprotobyname('tcp');
socket(SH, PF_INET, SOCK_STREAM, $proto) or my_die("socket:",$!);

$iaddr = gethostbyname($host);
$port = getservbyname('http', 'tcp') || 80;
$sin = sockaddr_in($port, $iaddr);
connect(SH, $sin) or die "connect: $!\n";

$path ||= '/';
$sent = send(SH, "HEAD $path HTTP/1.0\n\n", 0, $sin) or my_die("send:",$!);

if (recv(SH, $buf, 1024, 0)) {
    ($x, $result) = split /\s+/, $buf;
    $exist = ($result == 200) ? 1 : 0;
} else {
    die "recv: $!";
}

#print "exist = $exist\n";
#I do not want to print the value $exist.
close SH;
Avatar of mmcw

ASKER

The screen becomes white and that is all it does when I try to use the part where I edited the code you entered!

How to check if my provider uses port 80??
What is the value of $url (as assigned in the first line)?
Avatar of mmcw

ASKER

http://mmcw.hypermart.net/Images/302.gif

but I want to check also if ".txt" and ".lang" are availeble.

Is this possible??
Avatar of mmcw

ASKER

Sorry, amistype!!

http://mmcw.hypermart.net/Images/302.gif 

but I want to check also for ".txt" and ".lang" files.

Is this possible??
I ran script with the above URL and it showed the value of $exist as '0'.

> The screen becomes white and that is all it does when I try to
> use the part where I edited the code you entered!

Since you are not printing anything in the script, there is no output. The screen would have to be blank. What kind of output are you expecting?

Are you running it as command line script? or as a CGI script?

> but I want to check also for ".txt" and ".lang" files.

You need to construct the URL appropriately (with the right file name etc.). Then you can check for any type of files.
Avatar of mmcw

ASKER

I am maybe a dummy but what do you mean by:

You need to construct the URL appropriately (with the right file name etc.). Then you can check for any type of files.

Can you help me??

greetings Michel
I meant just the way you assigned a value to $url (in your script):

> $url = "$imgurl/$r_rowdata->{$_}";

Make sure the URL has the all the required components (like the "http:" part, the host name etc.)