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
On Aug 15, 2006, at 2:16 PM, Neil Charlton wrote:
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.
On Wed, 16 Aug 2006, Neil Charlton wrote:
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::open() 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 H.