?
Solved

Check if file exists with only the URL

Posted on 2001-06-21
5
Medium Priority
?
309 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

755 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