Solved

Check if file exists with only the URL

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading fields from the text file. 4 97
unable to remove invisible characters with perl 2 85
Migrate OTRS to newest version. 1 334
html form to write data to csv 24 114
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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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