Solved

Check if file exists with only the URL

Posted on 2001-06-21
5
261 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:netje
5 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 6213797
# 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;
     close(FILE);
}
else
{
     print("\nOh dear .. i couldnt get the url...");
}
0
 
LVL 5

Accepted Solution

by:
Sapa earned 50 total points
ID: 6215474
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
0
 
LVL 5

Expert Comment

by:dcgames
ID: 6216041
Does this work only for HTML files? (the header part)..

Dave
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6216681
"Does this work only for HTML files?"

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

- Sapa
0
 
LVL 2

Expert Comment

by:shirjeel
ID: 6223586
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 => 'http://www.perl.com/cgi-bin/BugGlimpse';
  $req->content_type('application/x-www-form-urlencoded');
  $req->content('match=www&errors=0');
  # 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 http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/site/lib/LWP.html

will give u full info abot it
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now