# FileTest.exists? over HTTP

**URL:** <https://www.ruby-forum.com/t/filetest-exists-over-http/65040>\
**Category:** Ruby\
**Created:** [August 15, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/filetest-exists-over-http/65040 "2006-08-15T20:16:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 15, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/filetest-exists-over-http/65040/1 "2006-08-15T20:16:36Z")

</div>

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?(“[Website Domain Names, Online Stores & Hosting | Domain.com](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

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 16, 2006, 6:52am UTC](https://www.ruby-forum.com/t/filetest-exists-over-http/65040/2 "2006-08-16T06:52:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 15, 2006, 8:27pm UTC](https://www.ruby-forum.com/t/filetest-exists-over-http/65040/3 "2006-08-15T20:27:20Z")

</div>

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(“[Website Domain Names, Online Stores & Hosting | Domain.com](http://domain.com/image.jpg)”)  
rescue OpenURI::HTTPError

# It couldn’t be accessed, so do something.

end

Kirk H.
