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;
mmcwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmcwAuthor Commented:
Edited text of question.
0
mjswartCommented:
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
0
khacharnCommented:
hi mmcw
i think you can conveniently use
(if -e (pathoffilename)) to check if the file EXISTS..
hope this help
Regards
Nitin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

mmcwAuthor Commented:
Where do I get more information about HTTP::request???
0
mjswartCommented:
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
0
mjswartCommented:
Remember to reject the other answer if you find that mine is more apropriate.
Cheers
M
0
khacharnCommented:
Well well..
mmcw..
i hope you try my suggestion too..
i feel that it will slove your problem easily..
nitin
Regards
0
prakashk021799Commented:
'-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;
0
mjswartCommented:
good answer
0
mmcwAuthor Commented:
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!
0
prakashk021799Commented:
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.
0
mmcwAuthor Commented:
I do not have.
Is there not another way??
0
prakashk021799Commented:
## 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;
0
mmcwAuthor Commented:
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;
0
mmcwAuthor Commented:
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??
0
prakashk021799Commented:
What is the value of $url (as assigned in the first line)?
0
mmcwAuthor Commented:
http://mmcw.hypermart.net/Images/302.gif

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

Is this possible??
0
mmcwAuthor Commented:
Sorry, amistype!!

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

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

Is this possible??
0
prakashk021799Commented:
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?

0
prakashk021799Commented:
> 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.
0
mmcwAuthor Commented:
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
0
prakashk021799Commented:
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.)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.