Check if file exists with only the URL

Is it possible to check if a file exists when only the URL to this file is available. (NOT THE PATH to the file)

With the path I know it is possible to use something like -e $file.
Who is Participating?
SapaConnect With a Mentor Commented:
it's better to use HEAD method because the previous one will not work with zero-lenght files and will create big traffic and take much time with long files.

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

# ...

my $ua=new LWP::UserAgent;
my $req=new HTTP::Request(HEAD=>$url);
my $resp=$ua->request($req);
if ($resp->is_success) {
    # ... it exists!
} else {
    # ... sorry :-(

- Sapa
# written by paul maker
# simple example of the LWP::Simple package, standard in activestate perl build 5.004

# pss it the url of the file, including http://

use CGI;
use LWP::Simple;

print("Im Getting ".$ARGV[0]."..");

if($content = get($ARGV[0]))
     open(FILE,"> temp.html") or die($!);
     print FILE $content;
     print("\nOh dear .. i couldnt get the url...");
Does this work only for HTML files? (the header part)..

"Does this work only for HTML files?"

No. For any ones. It returns HTTP (hot HTML) header.

- Sapa
use this man,

# Create a user agent object
  use LWP::UserAgent;
  $ua = new LWP::UserAgent;
  $ua->agent("AgentName/0.1 " . $ua->agent);
  # Create a request
  my $req = new HTTP::Request POST => '';
  # Pass request to the user agent and get a response back
  my $res = $ua->request($req);
  # Check the outcome of the response
  if ($res->is_success) {
      print $res->content;
  } else {
      print "Bad luck this time\n";

and go to the link of

will give u full info abot it
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.