FileTest.exists? over HTTP

N

Neil Charlton

HI

I would like to test for the existence of a file over HTTP. I have tried
various combinations of File.stat and FileTest.exists? with no results.

For example (image.jpg exists)

if FileTest.exists?("http://domain.com/image.jpg")
puts "found file"
end

does not print found file even though the file exists

What am I doing wrong ?

Thanks in Advance
 
K

khaines

HI

I would like to test for the existence of a file over HTTP. I have tried
various combinations of File.stat and FileTest.exists? with no results.

For example (image.jpg exists)

if FileTest.exists?("http://domain.com/image.jpg")
puts "found file"
end

does not print found file even though the file exists

What am I doing wrong ?

Using FileTest.exists?() for an HTTP operation.

By default, Ruby doesn't try to make file operations transparently work
over HTTP, except for the availability of the open-uri in the ruby core,
which lets one use Kernel::eek:pen() to open http, https, and ftp URIs.

The easiest way, I think, to get the simple effect you are looking for is
to use open-uri.

require 'open-uri'
begin
open("http://domain.com/image.jpg")
rescue OpenURI::HTTPError
# It couldn't be accessed, so do something.
end



Kirk Haines
 
L

Logan Capaldo

HI

I would like to test for the existence of a file over HTTP. I have
tried
various combinations of File.stat and FileTest.exists? with no
results.

For example (image.jpg exists)

if FileTest.exists?("http://domain.com/image.jpg")
puts "found file"
end

does not print found file even though the file exists

What am I doing wrong ?

As Kirk mentioned, the only transparent url support is with open-uri.
Also File.exists? does not really make sense for a url. A given url
does not necessarily point to a real file. Does it exist if you get a
response code of 200? What about 302? What about 404? Sure the
webserver says it doesn't exist, but you do get some content back
usually.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,089
Members
47,687
Latest member
IngridXxj

Latest Threads

Top