Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Check if file exists with only the URL

Posted on 2001-06-21
5
286 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

860 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